我試圖檢索所有與今天的日期相匹配的記錄,但是當我運行
時,我得到了這個例外System.NotSupportedException:"LINQ to Entities中不支持指定的型別成員'Date'。只有初始化器、物體成員和物體導航屬性被支持。"
我嘗試了這個解決方案 https://stackoverflow.com/a/14601775/13211556
但是沒有效果,同樣的例外,也嘗試將DB中的列改為Date,而不是DateTime。
這里是代碼:
List<int> Ids = context.Record
.Where(rec => DbFunctions.TruncateTime(rec.Date.Date) == date)
.Select(rec => rec.Id)
.ToList()。
uj5u.com熱心網友回復:
為了提高性能,你必須使用比較運算子。你有date,但沒有時間,所以添加一天,并寫適當的LINQ查詢。
var nextDay = date.AddDays(1) 。
List<int> Ids = context.Record
.Where(rec => rec.Date >= date && rec.Date < nextDay)
.Select(rec => rec.Id)
.ToList()。
在這種情況下,只有索引
。轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/309849.html
標籤:
下一篇:物體框架一對多關系不作業
