【发布时间】:2021-08-09 22:22:01
【问题描述】:
我有一个与 1+ 个 rating 实体相关的 event 实体。我需要查询我的数据库以仅获取(例如)在此特定事件的总评分中平均评分为 2 星的事件。 Rate1、2、3、4 的值可以在 [0,1,2,3,4,5] 颗星之间。
public class Event
{
public int Id { get; set; }
public string Title { get; set; }
public virtual List<Rating> Ratings { get; set; }
}
public class Rating
{
public int Id { get; set; }
public int Rate1 { get; set; }
public int Rate2 { get; set; }
public int Rate3 { get; set; }
public int Rate4 { get; set; }
public virtual Event Event { get; set; }
}
例如:1 个事件与 3 个评分相关,如下所示:
编号:1 率 1:3 率 2:2 率 3:5 率 4:1
编号:2 评分 1:4 率 2:1 率 3:0 率 4:1
编号:3 率 1:3 率 2:1 率 3:3 率 4:2
平均值计算如下: ( (3+2+5+1) + (4+1+0+1) + (3+1+3+2) ) / 12 = 2,16(四舍五入)到 2)
我想知道是否可以编写一个 Linq 查询来获取所有被评为 2 的事件?
【问题讨论】:
标签: linq entity-framework-core