如何将下面的sql查询转换为linq?
SELECT
COUNT(CASE WHEN P.Archived = '1' THEN 1 END) As ACount,
COUNT(CASE WHEN P.IsImportant = '1' and P.Archived = '0' THEN 1
END) As ICount,
COUNT(CASE WHEN P.CreatedById = 1389 and P.Archived = '0' THEN 1 END) As CCount,
COUNT(CASE WHEN P.CreatedById != 1389 and P.Archived = '0' THEN 1 END) As SCount,
COUNT(*) as TotalThreatModelsCount
FROM Parties P
这不会产生完全相同的查询,因为它会导致对数据库的5个单独查询,但它会给出相同的结果。
using (YourContext context = new YourContext())
{
int ACount = context.Parties.Count(_ => _.Archived == "1");
int ICount = context.Parties.Count(_ => _.IsImportant == "1" && _.Archived == "0");
int CCount = context.Parties.Count(_ => _.CreatedById == 1389 && _.Archived == "0");
int SCount = context.Parties.Count(_ => _.CreatedById != 1389 && _.Archived == "0");
int TotalThreadModelsCount = context.Parties.Count();
}