【问题标题】:Get objects from collection A where the ID of A exists in a collection of B objects从集合 A 中获取对象,其中 A 的 ID 存在于 B 对象的集合中
【发布时间】:2012-08-30 13:23:24
【问题描述】:

我有一个人对象的集合 A,并且这个人有一个名为“ID”的属性。 我还有一个由 personcountry 对象组成的集合 B,并且 personcountry 对象有一个属性 personId。

现在我想选择集合 A 中的所有人员,集合 B 中有一个对象及其 personId。

所以有了这些数据

person (collection A List[Person]) 
person id  name
    1      John
    2      John
    3      John2
    4      Pete
    5      Bill
    6      Samantha

还有这个集合 B (List[Country])

country id  personid
1             1
1             3
2             5

我想要集合 A 中 id 为 1,3 和 5 的人员记录。

我确实设法使用 intersect 方法使用 2 个整数集合来做到这一点,但是在使用这两个对象集合时我被卡住了。

【问题讨论】:

    标签: c# linq


    【解决方案1】:
     var q = Persons.Where(p => Countries.Any(c => p.Id== c.PersonId))
    

    【讨论】:

      【解决方案2】:

      我会加入

      var query = (from p in persons
                  join pc in personcountry on p.Id equals pc.PersonId
                  select p).AsEnumerable();
      

      【讨论】:

        猜你喜欢
        • 2016-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-23
        • 1970-01-01
        • 2019-04-17
        相关资源
        最近更新 更多