LINQ 加入帮助

发布于 2024-10-25 12:23:32 字数 417 浏览 3 评论 0原文

var q = (from Labels in dc.tblArtworkDataLabels select Labels).ToList();

但我需要这样做相当于:

SELECT     d.ID, d.labelID, d.dataID, d.data, l.templateID
FROM         tblArtworkDataLabels AS d INNER JOIN
                      tblArtworkData AS l ON d.dataID = l.ID
WHERE     (l.templateID = 238)

如何在 LINQ 中执行此操作?

编辑

抱歉!错过了原始 statmenet 上的 WHERE 子句!

var q = (from Labels in dc.tblArtworkDataLabels select Labels).ToList();

But I need this to do the quivalent of:

SELECT     d.ID, d.labelID, d.dataID, d.data, l.templateID
FROM         tblArtworkDataLabels AS d INNER JOIN
                      tblArtworkData AS l ON d.dataID = l.ID
WHERE     (l.templateID = 238)

How do I do this in LINQ?

Edit

Sorry! Missed the WHERE clause on original statmenet!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半边脸i 2024-11-01 12:23:32
var result = dc.tblArtworkDataLabels
    .Join(dc.tblArtworkData, l => l.ID, d => d.dataID, (l, d) => new {l, d})
    .Select(o => new {
         Id = o.d.ID,
         LabelId = o.d.labelID,
         DataId = o.d.dataID, 
         Data = o.d.data, 
         TemplateId = o.l.templateID,
     })
    .Where(o => o.l.templateID == 238);
var result = dc.tblArtworkDataLabels
    .Join(dc.tblArtworkData, l => l.ID, d => d.dataID, (l, d) => new {l, d})
    .Select(o => new {
         Id = o.d.ID,
         LabelId = o.d.labelID,
         DataId = o.d.dataID, 
         Data = o.d.data, 
         TemplateId = o.l.templateID,
     })
    .Where(o => o.l.templateID == 238);
软糖 2024-11-01 12:23:32

如果您在 tblArtworkData 上有一个正确的外键到 tblArtworkDataLabels 上的主键,并且已将它们正确导入到 DBML 设计器中,您可以让 LINQ2SQL 隐式创建联接:

from l in tblArtworkData
where l.templateID = 238
select new {
    Id = l.tblArtworkDataLabel.ID,
    LabelId = l.tblArtworkDataLabel.labelID,
    DataId = l.tblArtworkDataLabel.dataID,
    Data = l.tblArtworkDataLabel.data,
    TemplateId = l.templateID,
}

请参阅< href="https://stackoverflow.com/questions/5307731/linq-to-sql-multiple-joins-on-multiple-columns-is-this-possible/5309754#5309754">我的回答问题“LINQ to SQL:多个列上的多个联接。这可能吗?”了解隐式连接如何转换为 SQL。

编辑:

如果我误解了您的关系,并且您有许多 tblArtworkDataLabels 到一个 tblArtworkData,您必须以相反的方式进行查询

from d in tblArtworkDataLabels
where d.tblArtworkData.templateID = 238
select new {
    Id = d.ID,
    LabelId = d.labelID,
    DataId = d.dataID,
    Data = d.data,
    TemplateId = d.tblArtworkData.templateID,
}

If you have a correct foreign key on tblArtworkData to the primary key on the tblArtworkDataLabels and have imported them correctly into the DBML designer you can have LINQ2SQL implicitly creating the join:

from l in tblArtworkData
where l.templateID = 238
select new {
    Id = l.tblArtworkDataLabel.ID,
    LabelId = l.tblArtworkDataLabel.labelID,
    DataId = l.tblArtworkDataLabel.dataID,
    Data = l.tblArtworkDataLabel.data,
    TemplateId = l.templateID,
}

See my answer on the question "LINQ to SQL: Multiple joins ON multiple Columns. Is this possible?" for how the implicit join translates to SQL.

Edit:

In the case I misunderstood your relations and you have many tblArtworkDataLabels to one tblArtworkData you have to turn the query the other way around

from d in tblArtworkDataLabels
where d.tblArtworkData.templateID = 238
select new {
    Id = d.ID,
    LabelId = d.labelID,
    DataId = d.dataID,
    Data = d.data,
    TemplateId = d.tblArtworkData.templateID,
}
转身泪倾城 2024-11-01 12:23:32

尝试

var q = (from Labels in dc.tblArtworkDataLabels
         join data in dc.tblArtworkData on Labels.ID equals data.DataID select Labels).ToList();

try

var q = (from Labels in dc.tblArtworkDataLabels
         join data in dc.tblArtworkData on Labels.ID equals data.DataID select Labels).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文