![](/img/trans.png)
[英]EF Code First change data type bool? to bool and int? to int
[英]EF code first cannot convert type to int
我正在餐廳評論網站上創建一個功能,該功能獲取上次評論的美食,然后查找平均評分較高的其他具有相同美食的評論。 然后,它將每個餐廳ID保存在數據庫中。 但是我繼續收到錯誤, Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int'
where r.id = samecuisine
的行where r.id = samecuisine
Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int'
。
任何幫助將不勝感激。
var samecuisine = (from r in db.Reviews
where r.Cuisine == review.Cuisine
select r.id);
var pick = (from r in db.Reviews
where r.id = samecuisine
select r.score).Average();
var choices = (from r in db.Reviews
where r.score >= pick
select r.RestaurantId).ToList();
目前尚不清楚為什么您要獲得評論ID。 您只對這些評論的分數感興趣,對嗎? 我懷疑您實際上是要參加第一個查詢,第二個查詢是一部分:
var averageScoreForCuisine = (from r in db.Reviews
where r.Cuisine == review.Cuisine
select r.score).Average();
或者我更容易理解:
var averageScoreForCuisine = db.Reviews
.Where(r => r.Cuisine == review.Cuisine)
.Average(r => r.score);
您的查詢
var samecuisine = (from r in db.Reviews
where r.Cuisine == review.Cuisine
select r.id);
返回一個IQueryable<int>
,而不是實際的int
。
嘗試檢索.FirstOrDefault()
,這將產生第一個結果(或默認值0)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.