2017-08-19 62 views
0

我希望得到兩個時間之間的所有記錄:實體框架錯誤有關成員不支持

var Check1 = db.WorkingTs 
       .Where(x => DbFunctions.TruncateTime(x.WorkingTime).Value.TimeOfDay >= FTime.TimeOfDay 
         || DbFunctions.TruncateTime(x.WorkingTime).Value.TimeOfDay <= FBtime.TimeOfDay 
         && x.FlightsCid == Crewstuff.id) 
       .FirstOrDefault(); 

,但我得到了以下錯誤:

The specified type member 'Time Of Day' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported

我也試過DbFunctions.CreateTime(),但同樣的錯誤仍然出現。

謝謝

回答

1

先創建新的DateTime變量。然後在你查詢時使用它。

var fTime = FTime.TimeOfDay; 
var bTime = FBtime.TimeOfDay; 

var Check1 = db.WorkingTs.Where(x => 
DbFunctions.CreateTime(x.WorkingTime.Hour, x.WorkingTime.Minute, x.WorkingTime.Second) >= fTime 
|| DbFunctions.CreateTime(x.WorkingTime.Hour, x.WorkingTime.Minute, x.WorkingTime.Second) <= bTime 
&& x.FlightsCid == Crewstuff.id).FirstOrDefault(); 

順便說一句,你可以使用FirstOrDefault(),而不是在哪裏()

var fTime = FTime.TimeOfDay; 
var bTime = FBtime.TimeOfDay; 

var Check1 = db.WorkingTs.FirstOrDefault(x => 
DbFunctions.CreateTime(x.WorkingTime.Hour, x.WorkingTime.Minute, x.WorkingTime.Second) >= fTime 
|| DbFunctions.CreateTime(x.WorkingTime.Hour, x.WorkingTime.Minute, x.WorkingTime.Second) <= bTime 
&& x.FlightsCid == Crewstuff.id); 
+0

你仍然不能做'.Value.TimeOfDay'。 – Igor

+0

剛編輯答案。這應該工作 –

+0

非常感謝@IlyaCosmosTaraskin工作! –