2016-06-28 9 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 }これは可能ですか?ありがとうIF文をLINQのWHERE句に追加する方法は?

答えて

2

と同様にHimBromBeereの答えを、それがこの

のようにも達成することができます
where tbl.AutoAuditNotes != Constants.PUBLISH_AUDIT_STRING 
    && tbl.AutoAuditNotes != string.Empty 
    && (!isLatest_ || anotherCondition) 

誰かがこれをより読みやすいと思うかもしれませんが、それは味の問題です。 (isLatest_true場合)isLatestfalse又はある場合

最後&&部分はtrueあるanotherConditionに依存します。

+0

クールな解決策、私にはもう少し明白なようです+1 – HimBromBeere

1

確かに、isLatest_falseの場合は、3進演算子を使用してtrueを追加してください。 trueは、すべての以前の条件が満たされたときにテストに合格するようにします。

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

あなたは 'true'を意味すると思います。' isLatest_'がfalseの場合、条件は適用されるべきではないので、常に「真の」状態である必要があります。 –

+0

@JonSkeetありがとうございました。 – 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' .. –