سؤال

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?

هل كانت مفيدة؟

المحلول

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top