2016-06-28 115 views
0
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_。如果这个变量的值是真的,那么我需要在'where'子句中添加另一个条件,例如:where tbl.AutoAuditNotes != Constants.PUBLISH_AUDIT_STRING && tbl.AutoAuditNotes != string.Empty && if (isLatest_) { // another condition }这可能吗?谢谢如何在LINQ的WHERE子句中添加IF语句?

回答

2

除了HimBromBeere的回答,也可以像这样

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

也许有人认为这更可读,但这是一个品味的问题。

最后&&部分是true如果isLatestfalse(如果isLatest_true)取决于anotherCondition

+0

很酷的解决方案,对我来说似乎更加明显+1 – HimBromBeere

1

当然,只要使用三元运算符并追加true如果isLatest_也是false也。 true确保测试通过所有以前的条件通过。

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

我认为你的意思是'true' - 如果'isLatest_'是假的,则不应该应用条件,所以它应该就好像它是一个“永远为真”的条件。 –

+0

@JonSkeet谢谢Jon。 – HimBromBeere

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 
+0

我觉得你很困惑:如果'isLatest_'为'true',则应该应用othercondition ...如果'isLatest_'为'true'则返回'true',并且只有'isLatest_'时才使用'othercondition' 'false' .. –