Alex Rivera | Logout

Query a List of List of Items in LINQ c#

Asked 2010-05-08T17:18:46.340
14

I am a bit new to LINQ, here is my problem.

  • I have a List of List of Items
  • I like to get the Items which are present in only one List (and if I could get the List in which they are without re-iterating through the "list of list" that would be great).

I am trying without success to use the Aggregate / Except / Group keywords in the Linq query but nothing close to a solution so far.

[EDIT] Could be for instance List<List<int>> and the condition that the value of the int is is not in the others lists.

To be honest if I tried with several foreach I succeed to find the value items but as I am trying to learn LINQ I would like to know what kind of query I should write to get the results

for instance

 1,2,6
 1,6
 3,5
 5,10
 3,10,6

will return 2 and the first list

Edit
Report

1 Answer

2
from lst in lists
from i in lst
group lst by i into grp
where grp.Count() == 1
select new { Value = grp.Key, List = grp.Single() };

That will give you the numbers that appear only in 1 list, along with the list that contains it

EDIT: simplified a bit

answered 2010-05-08T18:26:56.307

Your Answer