1
我有一个表:LINQ编译的查询选择和多列
ForObjectTypeID (short, PK)
ForObjectID (int, PK)
UserID (int, PK)
Upvote (bool)
ShadowBannedVote (bool)
给定一个ObjectTypeID
和ObjectID
,我想返回Tuple<int, int, int>
其中相应的值是:
- 总票数:其中
ShadowBannedVote == false
- 总记录数:的记录总数,其中
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)));