Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a LINQ join select only the first record?

I wish to select only the first record from the 'CustomerSubOwners' table in join query below and wondered what was the best way to achieve this in LINQ.

var result= (from t1 in db.Cases
             from t2 in db.CustomerSubOwners
                          .Where(o => t1.CustomerId == o.CustomerId && o.Expiry >= DateTime.Now)
                          .DefaultIfEmpty()
             select t1);
like image 771
Nick Avatar asked Oct 15 '12 16:10

Nick


1 Answers

I think you are looking for the Take method like so:

var result= (from t1 in db.Cases
             from t2 in db.CustomerSubOwners.Where(o => t1.CustomerId == o.CustomerId && o.Expiry >= DateTime.Now)
                                            .Take(1)
                                            .DefaultIfEmpty()
             select t1); 
like image 76
Aducci Avatar answered Sep 27 '22 23:09

Aducci