-1

我有两个不同类型的集合:

// First Collection
public class ObjectA
{
   public IEnumerable<SomeObject> Persons { get; set; }
   public IEnumerable<SomeOtherObject> Items { get; set; }
}

public class SomeObject
{
   public string ID { get; set; }
}
public class SomeOtherObject
{
   public string ID { get; set; }
}

// Second Collection (ID property holds unique values, PersonID and EntityID do not)
public class ObjectB
{
    public string ID { get; set; }
    public string PersonID { get; set; }
    public string EntityID{ get; set; }
}

// Assuming the following entries...
var list1 = new ObjectA();
var persons = new List<SomeObject>();
persons.Add(new SomeObject() { ID = "A" });
persons.Add(new SomeObject() { ID = "B" });
persons.Add(new SomeObject() { ID = "C" });
list1.Persons = persons;

var items = new List<SomeOtherObject>();
items.Add(new SomeOtherObject() { ID = "1" });
items.Add(new SomeOtherObject() { ID = "2" });
list1.Items = items;

var list2 = new List<ObjectB>();
list2.Add(new ObjectB() { ID = "1", PersonID = "C", EntityID = "1" });
list2.Add(new ObjectB() { ID = "2", PersonID = "A", EntityID = "1" });
list2.Add(new ObjectB() { ID = "3", PersonID = "A", EntityID = "2" });
list2.Add(new ObjectB() { ID = "4", PersonID = "B", EntityID = "1" });
list2.Add(new ObjectB() { ID = "5", PersonID = "B", EntityID = "2" });

给定这两个列表(list1 和 list2)。如何在 list2 中查找 PersonID = A AND B AND EntityID = 1 AND 2 的项目

换句话说,必须满足所有条件,所以“人 C”不会成功,因为他们只有 EntityID =“1”,而不是“1”和“2”

4

6 回答 6

1

你可以使用这样的东西

list2.Where(b => list1.Persons.Exists(person => person.ID == b.PersonID) &&
                 list1.Items.TrueForAll(id => list2.Exists(bs => bs.EntityID == id.ID && bs.PersonID == b.PersonID)))

这将给出 list2 中的 4 个项目(如果需要,您可以获取唯一的 Persons)

于 2012-12-19T10:42:02.807 回答
1

如果我没看错问题,这个查询将为您提供包含 list1.Items 中列出的所有 EntityIds 的所有人员 ID(而不是硬编码 1 和 2)

var validIds = list1.Items.Select(i => i.ID);
var validPersonIds = list2.GroupBy(p => p.PersonID)
                          .Where(g => validIds.All(i => g.Any(x => x.EntityID == i)))
                          .Select(g => g.Key);

更新:根据您的评论,我认为这就是您所需要的。它从 list2 返回 ID 为 2、3、4 和 5 的对象

var validEntityIds = list1.Items.Select(i => i.ID);
var validPersonIds = list1.Persons.Select(i => i.ID);
var validObjects = list2.GroupBy(p => p.PersonID)
                        .Where(g => validPersonIds.Contains(g.Key) &&
                                    validEntityIds.All(i => g.Any(x => x.EntityID == i)))
                        .SelectMany(g => g);
于 2012-12-19T10:59:19.497 回答
0

试试这个:这会给你你想要的组合。

        var listSelected = (from val2 in list2
                            where val2.PersonID == "A" || val2.PersonID == "B"
                            && val2.EntityID == "1" || val2.EntityID == "2"
                            select val2).ToList();
于 2012-12-19T10:42:08.007 回答
0

也许这会产生你想要的:

  var result1 = list2.Where(row=> row.EntityID=="1").Select(r=>r.PersonID);
  var result2 = list2.Where(row => row.EntityID == "2").Select(r => r.PersonID);
  var finalResult = result1.Intersect(result2);
于 2012-12-19T10:43:55.313 回答
0

您可以尝试以下方法:

list2.Where(p => p.EntityID == "1" || p.EntityID == "2")
    .GroupBy(p => p.PersonID)
    .Where(grp => grp.Count() == 2);

list2这将返回具有指定实体 1 和 2的每个人;所以来自您的示例数据的“A”和“B”。

编辑:

根据您对 Azhar 的评论,以下内容更为通用:

var entityFilter = new string[]{"1", "2"};

list2.Where(p => entityFilter.Contains(p.EntityID))
    .GroupBy(p => p.PersonID)
    .Where(grp => grp.Count() == entityFilter.Count());
于 2012-12-19T10:50:02.197 回答
0
var query = list2.GroupBy(item => item.PersonID)
                 .Where(g => g.Any(item => item.EntityID == "1") &&
                             g.Any(item => item.EntityID == "2"))
                 .SelectMany(g => g);

返回除第一个以外的所有项目,因为没有 PersonID 等于CEntityID 等于 1 和 2 的项目。

于 2012-12-19T10:56:21.103 回答