0
public class Personel  
{
  public int Id {set;get;}
  public ICollection<Agreement> Agreements { set; get;}
}

public class Agreement
{
  public int Id {set;get;}
}

I have a domain model as above and have a dbcontext.

How do I get the last Agreement?

var result = _db.Personels.Include(a=>a.Agreements).OrderByDescending(x => x.Id);

I want to get all the personels and last agreement of them...

This gives me all the agreement of a personel, i want only the last one ordered by id descending.

DarthVader
  • 52,984
  • 76
  • 209
  • 300

1 Answers1

1
var result = 
from p in _db.Personels
select new {
        personel = p,
        lastAgreement = p.Agreements.OrderByDescending(x => x.Id).FirstOrDefault()
       }
.ToList();

Now you can convert this to list of Personels and set lastAgreement for each of those.

Andrew
  • 3,648
  • 1
  • 15
  • 29