简体   繁体   中英

Linq where list contains one of list

I have and array object that is a

List<ContactModel> contactList;

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

So objects would be

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

The ContactGroups contains a list of groups {"Group A", "Group B", "Group C","Group D"} that a given contact exists in. I can get results for one group by using the following.

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

Is there a way to get a list back with all objects containing one of a list of objects.

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

I need to get back object 1,2,4.

Target method:

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;
}

Examples of usage:

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

Help methods:

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);
}

This returns 1, 2, and 4:

var objects = new string[]  { "Group A", "Group B" };

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

Not sure if this is what you're looking for

Test :

    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 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM