繁体   English   中英

EF代码首先无法将类型转换为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM