Question

I am trying to select multiple table values using linq to sql

This is the code I wrote:

var query = (from p in context.Personel
join y in context.PerLanguage on p.ID equals y.ID
where p.Resign == false && p.KGBT > new DateTime(2012,1,15)
select new{ p.ID,p.NameSurname, y.EarnedDate,y.Degree}).ToList();

PerLanguage has a foreignkey "ID" to Personel. So PerLanguage table can have 2 or more data that has the same ID. I am expecting this piece of code to return me a List of items having the "last" entered Language data of different people.

What is the best way to do it?

Était-ce utile?

La solution

try the following query.. basically we make the join, get the flat results, group it by id and descending sort the results within an ID and select the first record in every grouped result.

var results = context.Personel.Where(p => !p.Resign && p.KGBT > new 
DateTime(2012,1,15)).Join(context.PerLanguage, p => p.ID, pl => pl.ID, (p, pl) => 
new { p.ID, p.NameSurname, pl.EarnedDate, pl.Degree }).GroupBy(r => r.ID)
.Select(g => g.OrderByDescending(r => r.EarnedDate).First()).ToList();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top