populate dictionary from the intersection of two lists

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
shiva455
Posts: 1
Joined: Tue 31 Jan 2012 03:48

populate dictionary from the intersection of two lists

Post by shiva455 » Tue 31 Jan 2012 03:55

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....

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Tue 31 Jan 2012 18:09

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.

jahanshazi
Posts: 1
Joined: Mon 25 Jun 2012 06:42

Re: populate dictionary from the intersection of two lists

Post by jahanshazi » Mon 25 Jun 2012 06:49

Keep up the good work

Post Reply