I came across linq query which ends in (…).ToList().AsQueryable(). I know the use of .ToList() and .AsQueryable() separately. But, what is the outcome if both are combined? When do we need to combine them? I hope this question is not too dump question. Thanks in advance.
First query:
Dim costAmounts = (From t In model.OrderTrades
Join o In model.Orders On t.OrdNum Equals o.OrdNum
Where o.ClientCode = clientNumber And o.BookDate = sysrec.ETicketsBookDate
Group t By t.Buy, t.OrdNum Into Amount = Sum(t.BuyAmount) _
Select OrdNum = OrdNum, Currency = Buy, SellAmount = Amount).ToList().AsQueryable()
Second Query:
Dim orders = (From a In costAmounts _
Group Join s In settlements On s.Currency Equals a.Currency _
And s.OrdNum Equals a.OrdNum Into amounts = Group _
From g In amounts.DefaultIfEmpty _
Where g Is Nothing OrElse a.SellAmount - g.Paid <> 0 _
Select OrdNum = a.OrdNum Distinct).ToList()
You should never need to combine the two.
AsQueryableis there to give you lazy evaluation features, which theToListcompletely negates.So, the LINQ query is forced to be evaluated by
ToList, then, the in memory list is converted to anIQueryablein order to allow… querying over it. Instead of constructing the query and only getting the results as needed.