J Antoni

J Antoni

  • NA
  • 81
  • 35.8k

C#LINQ Lambda -to get data comparing fields from two lists

Aug 20 2017 3:41 PM
I have 2 Lists
ListOne ==> ID , Name, Value
ListTwo ==> ID, Desc, Value
 
Expected Values of ListOne
[1,A,200]
[1,A,300]
[1,A,520]
[2,B,300]
[2,B,350]
[2,B,400]
[3,C,40]
[3,C,20]
 
Values of ListTwo-Master Data
[1,Analog,500]
[2,Benefit,310]
[3,Chain,50]
 
My Requirement
I want to get a list(say ResultantList ) that will contain only the value that do not exceed the value of its corresponding ID in the ListTwo.
 
So the ResultantList is expected to have only the below data from ListOne
[1,A,520] ===> the value here is 520, 520 exceeds the value(500) of ID 1 in ListTwo
[2,B,350] ===> the value here is 350, 350 exceeds the value(310) of ID 2 in ListTwo
[2,B,400] ===> the value here is 400, 400 exceeds the value(310) of ID 2 in ListTwo
===>[...]There is no value from ID 3 (name as C) in ListOne ;as both values(40,20) having ID 3 (name C) in ListOne is less than the value 50 with ID 3 (Desc Chain) in ListTwo
 
What I have tried
ResultantList = ListOne.Where(s => ListTwo.Any(l => (l.ID == s.ID && s.Value> l.Value))) .ToList();
But this fetches all the values even if the value is less than its corresponding value (w.r.t ID) in ListTwo.

Answers (2)