EF4 LINQ - 在联接上添加过滤

发布于 2024-10-27 07:19:56 字数 420 浏览 1 评论 0原文

如何在 LINQ 中将 FILTER CLAUSE 添加到 JOIN TABLE ....?

<块引用>

AND t2.Public = 1 AND t2.Color = '蓝色'


SELECT t1.ID, t1.Name, (SELECT COUNT(*) 
                      FROM tbl2 t2 
                     WHERE t2.ID = t1.ID 
                       AND t2.Public = 1 AND t2.Color = 'Blue') AS MyCount 
 FROM tbl1 t1
WHERE t1.State = 'CA'

How do I add a FILTER CLAUSE to a JOIN TABLE ....in LINQ ?

AND t2.Public = 1 AND t2.Color = 'Blue'


SELECT t1.ID, t1.Name, (SELECT COUNT(*) 
                      FROM tbl2 t2 
                     WHERE t2.ID = t1.ID 
                       AND t2.Public = 1 AND t2.Color = 'Blue') AS MyCount 
 FROM tbl1 t1
WHERE t1.State = 'CA'

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

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

发布评论

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

评论(2

吹梦到西洲 2024-11-03 07:19:56

它应该看起来像这样。

from t1 in context.tbl1
where t1.State = "CA"
select new 
{
    t1.ID, 
    t1.Name,
    MyCount = t1.tbl2s.Count(t2 => t2.Public && t2.Color == "Blue")
}

这就是我在不了解上下文中实体的名称、它们之间的关系以及它们的标量值的类型的情况下可以提供的所有帮助。

It should look something like this.

from t1 in context.tbl1
where t1.State = "CA"
select new 
{
    t1.ID, 
    t1.Name,
    MyCount = t1.tbl2s.Count(t2 => t2.Public && t2.Color == "Blue")
}

That's about all the help I can give without knowing more about the names of the entities in your context, their relationships to each other, and the types of their scalar values.

萌化 2024-11-03 07:19:56

如果您的实体之间没有导航属性,请使用:

var data = from t1 in context.Tbl1
           join t2 in context.Tbl2 on t1.Id equals t2.Id
           where t1.State = "CA" && t2.Public == 1 && t2.Color = "Blue"
           group t1 by new { t1.Id, t1.Name } into g
           select new 
               {
                   Id = g.Key.Id,
                   Name = g.Key.Name,
                   Count = g.Count()
               };

如果它们具有导航属性,请使用 @StriplingWarrior 提供的解决方案

If your entities don't have navigation property between them use:

var data = from t1 in context.Tbl1
           join t2 in context.Tbl2 on t1.Id equals t2.Id
           where t1.State = "CA" && t2.Public == 1 && t2.Color = "Blue"
           group t1 by new { t1.Id, t1.Name } into g
           select new 
               {
                   Id = g.Key.Id,
                   Name = g.Key.Name,
                   Count = g.Count()
               };

If they have navigation properties use solution provided by @StriplingWarrior

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文