簡體   English   中英

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

[英]How to Add IF statement in LINQ's WHERE Clause?

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 }有可能嗎? 謝謝

除了HimBromBeere的答案,也可以這樣實現

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

也許有人認為這更具可讀性,但這是一個品味問題。

如果isLatestfalse (如果isLatest_true )取決於anotherCondition則最后一個&&部分為true

當然,只要isLatest_也為false ,只需使用三元運算符並附加true true表示所有先前條件都通過時才能通過測試。

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

使用inline-if:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM