[英]EF code first cannot convert type to int
I am creating a function on my restaurant review website that gets the cuisine of the last review and then finds other reviews with same cuisine that have a higher average score. 我正在餐厅评论网站上创建一个功能,该功能获取上次评论的美食,然后查找平均评分较高的其他具有相同美食的评论。 It then saves every restaurant id in a database.
然后,它将每个餐厅ID保存在数据库中。 However I keep on receiving the error
Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int'
on the line where r.id = samecuisine
. 但是我继续收到错误,
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'
。
Any help would be grateful. 任何帮助将不胜感激。
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();
It's unclear why you'd want to get a review ID at all. 目前尚不清楚为什么您要获得评论ID。 You're only interested in the scores for those reviews, right?
您只对这些评论的分数感兴趣,对吗? I suspect you actually want to take part of your first query and part of your second:
我怀疑您实际上是要参加第一个查询,第二个查询是一部分:
var averageScoreForCuisine = (from r in db.Reviews
where r.Cuisine == review.Cuisine
select r.score).Average();
Or to my mind more readably: 或者我更容易理解:
var averageScoreForCuisine = db.Reviews
.Where(r => r.Cuisine == review.Cuisine)
.Average(r => r.score);
Your query 您的查询
var samecuisine = (from r in db.Reviews
where r.Cuisine == review.Cuisine
select r.id);
returns an IQueryable<int>
, not an actual int
. 返回一个
IQueryable<int>
,而不是实际的int
。
Try to retrieve the .FirstOrDefault()
, this yields the first result (or the default value 0). 尝试检索
.FirstOrDefault()
,这将产生第一个结果(或默认值0)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.