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

Dinesh Bali
Dinesh Bali used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
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-

Author

Commented:
Many thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial