[英]Use Last Method in Where Condition in LinQ to Entity
看這段代碼
IQueryable<Request> RequestsTotal = DataContext.Requests;
RequestsTotal = RequestsTotal.Where(rec =>
rec.RequestTransactions.Last().ServerStatusId != 0);
在執行此代碼期間,發生關於在Where條件中使用Last Method的錯誤。 我該如何解決這個問題?
UPDATE1 :這是發生的錯誤:
LINQ to Entities無法識別方法'DAL.RequestTransaction LastRequestTransaction',並且該方法無法轉換為商店表達式。
UPDATE2 :我有一個Request
表,並且此表與RequestTransactions
表具有一對多關系。 通過這個代碼,我試圖獲取所有requests
是沒有 ServerStatusId = 0
在這里它的輸入代碼的最后重新編碼RequestTransactions
。這有沒有其他辦法?
嘗試將Last()方法放在where之外:
Request newResult =
RequestsTotal
.Where(rec => rec.RequestTransactions.ServerStatusId != 0)
.Last();
最后還將執行查詢並返回單個Request對象而不是IQueryable。 因此,您無法為結果重復使用“ RequestsTotal”。
我將代碼更改為這種方式:
IQueryable<Request> RequestsTotal = DataContext.Requests;
List<Request> temp = new List<Request>();
foreach (Request request in RequestsTotal)
{
RequestTransaction last = request.RequestTransactions.LastOrDefault();
if (last != null && last.ServerStatusId != 0)
temp.Add(request);
}
RequestsTotal = temp.AsQueryable();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.