【问题标题】:Nested linq - where x == enumerable嵌套 linq - 其中 x == 可枚举
【发布时间】:2010-08-06 01:34:28
【问题描述】:

我是 Linq 和 SQL 术语的新手 - 谁能告诉我为什么这不起作用(语法不正确 - 我无法将 u.UserID int 与 Enumerable 进行比较)

var projectUsers = from u in SimpleRepository.All<User>()
                   where u.UserID == (from i in SimpleRepository.All<ProjectUser>()
                                      where i.ProjectID == p.ProjectID
                                      select i.UserID)
                   select u;

在“英语”中,我们会“选择其 id 与任何一个(来自 ProjectUser 集合,其中 projectID == x 的用户 ID)匹配的每个用户,并给我一个用户集合”。

如果这会有所不同(或者允许我使用其他东西来使这更容易),我也会将 subsonic3 与 SimpleRepository 一起使用。

【问题讨论】:

  • 请定义“不工作”。

标签: c# linq c#-3.0 subsonic3


【解决方案1】:
var projectUsers = from u in SimpleRepository.All<User>()
               where (from i in SimpleRepository.All<ProjectUser>()
                                  where i.ProjectID == p.ProjectID
                                  select i.UserID).Contains(u.UserID)
               select u;

var projectUsers = from u in SimpleRepository.All<User>()
               join u2 in SimpleRepository.All<ProjectUser>() on u.UserID equals u2.UserId
               where u2.ProjectID == p.ProjectID
               select u;

【讨论】:

  • 我们如何测试上面的 linq 表达式,因为我们没有引用 SimpleRepository 和其他对象?
  • @SoftwareGeek:我实际上并没有测试它们 :-S 但如果我这样做 - 那么我将创建整数列表并制作接近测试的东西。您不同意我们将 LINQ 用于对象还是整数并不重要?
  • 感谢 zerkms,我通过谷歌搜索第二个示例中的术语学到了一些知识。
  • @George R: msdn.microsoft.com/en-us/vcsharp/aa336746.aspx - 这也很有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
相关资源
最近更新 更多