Link to home
Start Free TrialLog in
Avatar of Gani tpt
Gani tpt

asked on

LINQ - How to search list values from another multiple list.

Hi,

How to find and search list values which is available or not from multiple another lists.

For example,

 List<string> FromList = new List<string>() { "Sub A", "Sub B" };
        List<string> List1 = new List<string>() { "Sub A=80", "Sub D=70" };
        List<string> List2 = new List<string>() { "Sub B=100", "Sub C=90" };

I want to take Fromlist and check this will be available or not in List1 and List2.

if it is available then it returns result as Sub A=80 and Sub B=100.

if it is not available in both list (List1 and List2) then it returns false.

How to form in LINQ Query..?
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

Try something like this? I only did this for list1. Just adapt it to include list2.
List<string> fromList = new List<string>(new string[] { "Sub A", "Sub B" });
List<string> list1 = new List<string>(new string[] { "Sub A=80", "Sub D=70" });
List<string> list = list1.Where(c => fromList.Any(c.Contains)).ToList();
if (list.Count() > 0)
{
    foreach (string item in list)
    {
     Console.WriteLine(item);   
    }
}
else
{
 Console.WriteLine("false");
 return false;   
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Code was tested and works as expected. Only solution provided