我正在尝试从SQL查询创建一个linq表达式。
我最大的问题是创建这一行:
CASE WHEN MIN() <> MAX() THEN 1 ELSE 0
在Linq表达式中。
SQL查询工作正常:
SELECT
CLD.Id, CLD.Comments,
CASE
WHEN MIN(AnswerComment.IdStatus) <> MAX(AnswerComment.IdStatus)
THEN 1 ELSE 0
END AS statusDifference
FROM
CLD
INNER JOIN
AnswerComment on CLD.Id = AnswerComment.IdCLD
INNER JOIN
ListDef on CLD.IdListDef = ListDef.Id
WHERE
ListDef.IdInspection = 1042
GROUP BY
CLD.Id, CLD.Comments
我正在尝试将此SQL查询转换为C#上的Linq表达式。
var sql = (from CLD in db.CLD
join AnswerComment in db.AnswerComment
on CLD.Id equals AnswerComment.IdCLD
join ListDef in db.ListDef
on CLD.IdListDef equals ListDef.Id
where ListDef.IdInspection == idInspec
group CLD by new ComentRespostaLFDModels
{
IdComment = CLD.Id,
Comment = CLD.Comments
} into coments
select new ComentRespostaLFDModels
{
IdComment = coments.Key.IdComment,
Comment = coments.Key.Comment
* Here I need to do the case: "CASE WHEN MIN(AnswerComment.IdStatus) <> MAX(AnswerComment.IdStatus) THEN 1 ELSE 0" or in other place that I don't know
}).ToList();
答案 0 :(得分:1)
您可以更改
之类的组子句var sql = (from CLD in db.CLD
join AnswerComment in db.AnswerComment on CLD.Id equals AnswerComment.IdCLD
join ListDef in db.ListDef on CLD.IdListDef equals ListDef.Id
where ListDef.IdInspection == idInspec
group new { CLD, AnswerComment } by new ComentRespostaLFDModels
{
IdComment = CLD.Id,
Comment = CLD.Comments
} into coments
select new ComentRespostaLFDModels
{
IdComment = coments.Key.IdComment,
Comment = coments.Key.Comment,
StatusDifference = comments.Min(c=>c.AnswerComment.IdStatus) != comments.Max(c=>c.AnswerComment.IdStatus) ? 1 : 0
}).ToList();
答案 1 :(得分:0)
var sql = (from CLD in db.CLD
join AnswerComment in db.AnswerComment
on CLD.Id equals AnswerComment.IdCLD
join ListDef in db.ListDef
on CLD.IdListDef equals ListDef.Id
where ListDef.IdInspection == idInspec
group new {CLD = CLD, AnswerComment = AnswerComment } by new ComentRespostaLFDModels
{
IdComment = CLD.Id,
Comment = CLD.Comments
} into coments
select new ComentRespostaLFDModels
{
IdComment = coments.Key.IdComment,
Comment = coments.Key.Comment,
StatusDifference = coment.Min(c => c.AnswerComent.IdStatus) != coment.Max(c => c.AnswerComent.IdStatus) //if you want to return bool or coment.Min() != coment.Max() ? 1 : 0 if you want to return int
}).ToList();
答案 2 :(得分:0)
试一试:
//Get all the ListDefIds where IdInspection == 1042
var listDefIds = db.ListDef.Where(x => x.IdInspection == 1042).Select(x => x.Id).ToList();
//filter CLD list by those ListDefId's
var filteredCLDList = db.CLD.Where(x => listDefIds.Contains(x.IdListDef)).ToList();
//filter answercomments by those whose IdCLD's are in the filtered CLD list
var filteredAnswerComments = db.AnswerComment.Where(x => filteredCLDList.Select(x => x.Id).Contains(x.IdCLD)).ToList();
//assuming status diff will always be same, calculate statusDiff
var statusDiff = filteredAnswerComments.Min(x.IdStatus) == filteredAnswerComments.Max(x.IdStatus) ? 0 : 1;
//filter CLD list again by filtered answercomments, then select new objects
var sql = filteredCLDList.Where(x => filteredAnswerComments.Select(x => x.IdCLD).Contains(x.Id))
.Select(x => new { Id = x.Id, Comments = x.Comments, statusDifference = statusDiff}).GroupBy(x => x.Id).ThenBy(x => x.Comments);