0

How can I search for an item in the string array in a list where these values can be in multiple columns.

using System;
using System.Collections.Generic;

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strtosearch = { "abc", "xyz", "123" };

        }
    }

I have a string array like above where I want to filter for these values from the below model.

    public class Source
    {
        public string S1 { get; set; }
        public ICollection<InnerSource> InnerSources { get; set; }
    }

    public class InnerSource
    {
        public string D1 { get; set; }
        public ICollection<InnerSource2> InnerSource2s  { get; set; }
    }
    public class InnerSource2
    {
        public string **D1ToSearch** { get; set; }
        public string D2 { get; set; }
        public string **D3ToSearch** { get; set; }
    }
}

Here in the first four with values D1ToSearch or D3ToSearch has the value abc and xyz . so my query result should limit to the first three entries.

var example = new List<Source>
            {
                new Source
                {
                    S1 ="1",
                    InnerSources = new List<InnerSource>
                    {
                        new InnerSource
                        {
                            D1 ="2",
                            InnerSource2s= new List<InnerSource2>
                            {
                                new InnerSource2
                                {
                                    D1ToSearch ="abc",
                                    D2="3",
                                    D3ToSearch="222"
                                },new InnerSource2
                                {
                                    D1ToSearch ="122",
                                    D2="3",
                                    D3ToSearch="abc"
                                }
                                ,new InnerSource2
                                {
                                    D1ToSearch ="abc",
                                    D2="3",
                                    D3ToSearch="333"
                                }

Here in the first four with values D1ToSearch or D3ToSearch has the value abc and xyz . so my query result should limit to the first three entries.

                                ,new InnerSource2
                                {
                                    D1ToSearch ="opq",
                                    D2="3",
                                    D3ToSearch="xyz"
                                },
                                ,new InnerSource2
                                {
                                    D1ToSearch ="arstbc",
                                    D2="3",
                                    D3ToSearch="uvw"
                                } } } } } };

If D1ToSearch or D3ToSearch contains any of the above values I would like to return that row.Above is a sample list added as an expamle .

How can I do this using LINQ query?

NetMage
  • 26,163
  • 3
  • 34
  • 55

1 Answers1

0

With nested lists, you can use SelectMany to collapse these and get to just the inner objects. Then you can do your OR comparison on the two fields you indicated, like so:

example
    .SelectMany(e => e.InnerSources.SelectMany(i2 => i2.InnerSource2s))     
    .Where(e => strtosearch.Contains(e.D1ToSearch) || strtosearch.Contains(e.D3ToSearch));
Carlo Bos
  • 3,105
  • 2
  • 16
  • 29