我有一张桌子:
ForObjectTypeID (short, PK)
ForObjectID (int, PK)
UserID (int, PK)
Upvote (bool)
ShadowBannedVote (bool)
给定一个ObjectTypeID
and ObjectID
,我希望返回一个Tuple<int, int, int>
各自的值是:
- 总投票数:记录的总数
ShadowBannedVote == false
- Total Upvotes:记录总数
Upvote == true && ShadowBannedVote == false
- 影子禁止投票总数:记录的总数
ShadowBannedVote == true
它需要是单个编译查询,而不是分解为多个查询。据我所知,我只是无法弄清楚如何在返回值中执行总和和计数。
public static readonly Func<DBContext, ObjectType, int, Tuple<int, int, int>> GetTotalVotes = CompiledQuery.Compile(
(DBContext db, ObjectType forObjectType, int forObjectID) =>
db.UserVotes.Where(c => c.ForObjectTypeID == (short)forObjectType && c.ForObjectID == forObjectID)
.Select(c=> new {c.Upvote, c.ShadowBannedVote}).Select(c=> new Tuple<int, int, in>(0, 0, 0)));