简体   繁体   中英

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();

I have the above code and I have a boolean variable isLatest_ . If the value of this varaible is true, then I need to add another condition inside the 'where' clause eg : where tbl.AutoAuditNotes != Constants.PUBLISH_AUDIT_STRING && tbl.AutoAuditNotes != string.Empty && if (isLatest_) { // another condition } Is that possible? Thanks

As well as HimBromBeere 's answer, it can also be achieved like this

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

Maybe someone sees this as more readable, but that's a matter of taste.

The last && part is true if isLatest is false or (if isLatest_ is true ) depends on anotherCondition .

Sure, simply use the ternary operator and append true if isLatest_ is false also. The true ensures that the test passes when all the former conditions pass.

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

use inline-if:

statement ? valueIfTrue : valueIfFalse

Add to you where : && (!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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM