Check if item contains from List item which is inside another list

Hi,

I am working on C# .Net project.

How I will make Linq query if I need to find the data from List object which is inside List?

public class CoreServicesRules
    {
        public List<CoreServicesProductDetails> CoreServicesProductList { get; set; }
    }


    public class CoreServicesProductDetails
    {   
        public List<string> ProductCodeList { get; set; }
        public string ReplaceProductCodeFromString { get; set; }
        public string ReplaceProductRateWithString { get; set; }
    }

Open in new window

     
      Now I wanted to check data from
      
	public List<string> ProductCodeList { get; set; }

Open in new window

     

      and I have object for CoreServicesRules
      
      
      I wanted to see if ProductCodeList contains "My product Code".
      
      Please help me with the query.
Dinesh BaliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Something like this:
var found = source.CoreServicesProductDetails.Any(d => d.ProductCodeList.Contains(code, StringComparer.OrdinalIgnoreCase));

Open in new window

Proof of concept:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EE_Q29136956
{
    class Program
    {
        static void Main(string[] args)
        {
            var rules = new CoreServicesRules
            {
                CoreServicesProductDetails = new List<CoreServicesProductDetails>
                {
                    new CoreServicesProductDetails { ProductCodeList = new List<string> { "1", "2", "3" } },
                    new CoreServicesProductDetails { ProductCodeList = new List<string> { "4", "5", "6" } },
                    new CoreServicesProductDetails { ProductCodeList = new List<string> { "7", "8", "9" } },
                    new CoreServicesProductDetails { ProductCodeList = new List<string> { "A", "b", "c" } }
                }
            };

            Console.WriteLine($"Rules contains 1: {rules.CoreServicesProductDetails.Any(d => d.ProductCodeList.Contains("1", StringComparer.OrdinalIgnoreCase))}");
            Console.WriteLine($"Rules contains z: {rules.CoreServicesProductDetails.Any(d => d.ProductCodeList.Contains("z", StringComparer.OrdinalIgnoreCase))}");
            Console.WriteLine($"Rules contains a: {rules.CoreServicesProductDetails.Any(d => d.ProductCodeList.Contains("a", StringComparer.OrdinalIgnoreCase))}");
            Console.ReadLine();
        }
    }

    class CoreServicesRules
    {
        public List<CoreServicesProductDetails> CoreServicesProductDetails { get; set; }
    }

    class CoreServicesProductDetails
    {
        public List<string> ProductCodeList { get; set; }
        public string ReplaceProductCodeFromString { get; set; }
        public string ReplaceProductRateWithString { get; set; }
    }
}

Open in new window

Produces the following output -Capture.PNG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dinesh BaliAuthor Commented:
Many thanks
Dinesh BaliAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.