Reputation: 5725
How do I create a LINQ statement that maintains deferred execution when the SQL query contains select top 1
? I want it to be chainable to other LINQ queries of type IQueryable<>.
var query1 = from t in table
where t.test == 1
select t;
//this should set query2 of type IQueryable<TableEntity>
var query2 = from q in query1
[[SELECT TOP 1 SOMEHOW]]
select q;
var query3 = from q in query2
where q.test2 == 2
var executedResult = query3.ToList();
Not fussed if the solution uses lambda or linq or both.
Upvotes: 1
Views: 127
Reputation: 4726
Use the Take(1) which returns an IEnumerable. If the source is empty this may return a null. Check these links out for more information...
MSDN source : http://msdn.microsoft.com/de-de/library/bb503062(v=vs.110).aspx
In your case
var query2 = (from q in query1
select t)
.Take(1);
Upvotes: 1
Reputation: 1251
You can try this:
var query2 = (from q in query1
select t)
.Take(1);
Take(count) returns IEnumerable containing count
elements (or less if it didn't find enough).
Upvotes: 3