我有以下示例列
ID Comment Analyzer Incubator Deanalyzer
-- ------- ------- --------- ----------
23 Need Fast 5.6 8.7
我需要的是显示结果是ID,评论,然后计数 有多少列有值。因此,在这种情况下,计数将为2,因为只有Analyzer和Deanalyzer具有值。这就是我到目前为止所做的:
var result = from tb in db.Reports
where tb.Id == 23
select new { ID = tb.ID,
Comments = tb.Comments,
Count = ..
}
答案 0 :(得分:1)
它不干净,但你可以这样做:
var result = from tb in db.Reports
where tb.Id == 23
select new { ID = tb.ID,
Comments = tb.Comments,
Count = (tb.Analyzer!= null ? 1 : 0) + (tb.Incubator != null ? 1 : 0) + (tb.Deanalyzer!= null ? 1 : 0)
}
如果您有很多列,我不会使用它。