2016-06-28 137 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' .. –