Linq中的where如何筛选包含列表中任意一个元素的项?

4

我有一个数组对象,它是一个

List<ContactModel> contactList;

public class ContactModel
{
    public string CustomKey { get; set; }
    public string[] ContactGroups { get; set; }
}

那么对象就是

{"1", {"Group A"} }
{"2", {"Group A", "Group B", "Group C"} }
{"3", {"Group C", "Group D"} }
{"4", {"Group A", "Group B", "Group C", "Group D"} }

ContactGroups 包含一个联系人存在于的组列表{"Group A", "Group B", "Group C","Group D"}。 我可以使用以下方式获取一个组的结果。

var selectedContracts =
    from contact in contacts
    where contact.ContactGroups.Contains("Group A")
    select new { contact.CustomKey, contact.ContactGroups };

有没有一种方法可以返回一个包含列表中任意一个对象的所有对象的列表。
var selectedContracts =
    from contact in contacts
    where contact.ContactGroups.Contains("Group A", "Group B")
    select new { contact.CustomKey, contact.ContactGroups };

我需要获取对象1、2、4。


4
请在格式上下点功夫……在预览中查看,直到它看起来符合您的期望以便于回答(提示:您的代码有一半缩进过多,另一半则过少……)。 - Jon Skeet
new[]{"Group A", "Group B"}.Any(x=>contact.ContactGroups.Contains(x)) - L.B
我无法理解你想要的输出是什么。你能根据你提供的输入更新一下期望的结果吗? - Rahul Singh
3个回答

2

目标方法:

public static IEnumerable<ContactModel> SelectContacts(
        List<ContactModel> contacts, string[] targetGroups)
{
    return from contact in contacts
           where targetGroups.Any(targetGroup => 
                 contact.ContactGroups.Contains(targetGroup))
           select contact;
}

使用示例:

void Run()
{
    Initial();
    var selectedContacts = SelectContacts(contactList, 
      new[] { "Group A", "Group B" });
    PrintContacts(selectedContacts);
}

帮助方法:

void Initial()
{
    contactList = new List<ContactModel>
    {
        new ContactModel
        {
            CustomKey = "1",
            ContactGroups = new[] { "Group A" }
        },
        new ContactModel
        {
            CustomKey = "2",
            ContactGroups = new[] { "Group A", "Group B", "Group C" }
        },
        new ContactModel
        {
            CustomKey = "3",
            ContactGroups = new[] { "Group C", "Group D" }
        },
        new ContactModel
        {
            CustomKey = "4",
            ContactGroups = new[] { "Group A", "Group B", "Group C", "Group D" }
        },
    };
}

void PrintContacts(IEnumerable<ContactModel> contacts)
{
    foreach (var selectedContract in contacts)
        Console.WriteLine(selectedContract.CustomKey);
}

谢谢,这正是我需要的。我之前并没有完全理解正确的语法。 - Jim

2
这将返回1、2和4:
var objects = new string[]  { "Group A", "Group B" };

var selectedContacts = contactList.Where(contact => objects
    .Any(obj => contact.ContactGroups.Contains(obj)));

0

不确定这是否是您要找的内容

测试:

    var contactList = new List<ContactModel> { new ContactModel {CustomKey = "1", ContactGroups = new[]{"Group A"} },
                                                new ContactModel {CustomKey ="2", ContactGroups = new[]{"Group A", "Group B", "Group C"} },
                                                new ContactModel {CustomKey ="3",ContactGroups = new[] {"Group C", "Group D"} },
                                                new ContactModel {CustomKey ="4", ContactGroups = new[]{"Group A", "Group B", "Group C", "Group D"}},
                                                new ContactModel {CustomKey ="5", ContactGroups = new[]{"Group A", "Group C", "Group D"}},
                                                new ContactModel {CustomKey ="6", ContactGroups = new[]{ "Group D"}},
                                                new ContactModel {CustomKey ="7", ContactGroups = new[]{"Group C"}},};

    var CheckFor = new List<string>{"Group A", "Group B"};

    var selectedContracts =
    from contact in contactList
    where contact.ContactGroups.Any(x => CheckFor.Contains(x))
    select new { contact.CustomKey, contact.ContactGroups };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接