Page 1 of 1

populate dictionary from the intersection of two lists

Posted: Tue 31 Jan 2012 03:55
by shiva455
Hi,

I have a list like List> X where Items is a class... and another List like List Y where

StudentTasks is a class which contains two variables PersonnelNumber(int),Iseligible(bool).......

In the above list X contains personnelnumber(int) which is the key for each list member in X...

In the above list Y contains personnelnumber(int) which is one of the variable in each list member (ie StudentTasks )...

actually the personnelnumbers in X were the subset of personnelnumbers present in Y which even contains personnal numbers

other than in X....So as now

Please let me knoiw how to fetch personnelnumbers from Y(which were present in X) and the associated Iseligible values from Y...

and need to create a new Dictionary like var q=new Dictionary() where this should be populated with values from the above resultset...ie...key as personnelnumbers and iseligible as value....

Posted: Tue 31 Jan 2012 18:09
by StanislavK
You can do this, e.g., in the following way:

Code: Select all

IEnumerable keys = X.Select(kvp => kvp.Key);
Dictionary dictionary = Y.Where(y => keys.Contains(y.PersonnelNumber))
  .Select(y => new { y.PersonnelNumber, y.IsEligible })
  .ToDictionary(n => n.PersonnelNumber, n => n.IsEligible);
JIC: as far as I can understand, this question is not related to LinqConnect and rather concerns the common LINQ to Objects functionality. Please use this forum for the questions on LinqConnect only.

Re: populate dictionary from the intersection of two lists

Posted: Mon 25 Jun 2012 06:49
by jahanshazi
Keep up the good work