How can a LINQ join select only the first record?

c# entity-framework linq linq-to-entities

Question

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);
1
14
7/19/2016 12:24:39 AM

Accepted Answer

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); 
33
10/15/2012 4:54:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow