Linq where list contains one of lists

I have and an array object which is

List<ContactModel> contactList;

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

      

So the objects will 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"} }

      

Contact groups contain a list of groups {"Group A", "Group B", "Group C", "Group D"} that this contact exists. I can get results for one group 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 return a list with all objects containing one of the 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 return object 1,2,4.

+3


source to share


3 answers


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 using:



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

      

+1


source


Not sure if this is what you are 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 };

      

0


source


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

      

0


source







All Articles