如何在LINQ的WHERE子句中添加IF语句?

时间:2016-06-28 05:30:42

标签: c# linq

var deliverableitems = (from tbl in GetContext.Deliverables.AsEnumerable()
                    where tbl.AutoAuditNotes != Constants.PUBLISH_AUDIT_STRING && tbl.AutoAuditNotes != string.Empty
                    select new CMChangeLogModel
                    {
                        RevisionDateTime = tbl.RevisionDateTime,
                        RevisionUser = tbl.RevisionUser,
                        Note =
                            (
                                tbl.AutoAuditNotes.Contains("Created") ? string.Format("Created Work Product '{0}'", hyperlinktemplate.Replace(pageidtemplate, tbl.DeliverableId.ToString()).Replace(pagetitletemplate, tbl.Title)) :
                                tbl.AutoAuditNotes.Contains("Changed") ? string.Format("Edited Work Product '{0}'", hyperlinktemplate.Replace(pageidtemplate, tbl.DeliverableId.ToString()).Replace(pagetitletemplate, tbl.Title)) :
                                tbl.AutoAuditNotes.Contains("has changed") ? string.Format("Edited Work Product '{0}'", hyperlinktemplate.Replace(pageidtemplate, tbl.DeliverableId.ToString()).Replace(pagetitletemplate, tbl.Title)) :
                                tbl.AutoAuditNotes.Contains("Added") ? string.Format("Edited Work Product '{0}'", hyperlinktemplate.Replace(pageidtemplate, tbl.DeliverableId.ToString()).Replace(pagetitletemplate, tbl.Title)) :
                                tbl.AutoAuditNotes.Contains("Removed") ? string.Format("Edited Work Product '{0}'", hyperlinktemplate.Replace(pageidtemplate, tbl.DeliverableId.ToString()).Replace(pagetitletemplate, tbl.Title)) :
                                tbl.AutoAuditNotes.Contains("Edited") ? string.Format("Edited Work Product '{0}'", hyperlinktemplate.Replace(pageidtemplate, tbl.DeliverableId.ToString()).Replace(pagetitletemplate, tbl.Title)) :
                                tbl.AutoAuditNotes.Contains("Deleted") ? string.Format("Deleted Work Product '{0}'", hyperlinktemplate.Replace(pageidtemplate, tbl.DeliverableId.ToString()).Replace(pagetitletemplate, tbl.Title)) :
                                tbl.AutoAuditNotes.Contains("Restored") ? string.Format("Restored Work Product '{0}'", hyperlinktemplate.Replace(pageidtemplate, tbl.DeliverableId.ToString()).Replace(pagetitletemplate, tbl.Title)) : "Unknown"
                            ),
                        Status = "AuditNote",
                        CM_PageId = tbl.CM_DeliverableId,
                        VMajor = tbl.VMajor,
                        VRevision = tbl.VRevision,
                        PageType = PageTypeEnum.Deliverable.ToString()
                    }).ToList();

我有上面的代码,我有一个布尔变量isLatest_。如果此变量的值为true,那么我需要在'where'子句中添加另一个条件,例如:where tbl.AutoAuditNotes != Constants.PUBLISH_AUDIT_STRING && tbl.AutoAuditNotes != string.Empty && if (isLatest_) { // another condition }这可能吗?感谢

3 个答案:

答案 0 :(得分:2)

HimBromBeere的答案外,还可以像这样实现

where tbl.AutoAuditNotes != Constants.PUBLISH_AUDIT_STRING 
    && tbl.AutoAuditNotes != string.Empty 
    && (!isLatest_ || anotherCondition)

也许有人认为这更具可读性,但这只是一种品味问题。

如果&&true (如果isLatestfalse,则isLatest_部分为true )取决于anotherCondition

答案 1 :(得分:1)

当然,如果true也是isLatest_,只需使用三元运算符并附加false即可。 true确保在所有前一个条件通过时测试通过。

where tbl.AutoAuditNotes != Constants.PUBLISH_AUDIT_STRING 
    && tbl.AutoAuditNotes != string.Empty 
    && isLatest_ ? anotherCondition : true

答案 2 :(得分:0)

使用内联 - 如果:

statement ? valueIfTrue : valueIfFalse

添加到您的位置:&& (!isLatest_ ? true : /* Add your condition here */)

from tbl in GetContext.Deliverables.AsEnumerable()
                where tbl.AutoAuditNotes != Constants.PUBLISH_AUDIT_STRING 
&& tbl.AutoAuditNotes != string.Empty 
&& (isLatest_ ? true : /* Add your condition here */)
                    select new CMChangeLogMode