simpleProgrammer
simpleProgrammer

Reputation: 189

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

Upvotes: 0

Views: 79

Answers (3)

René Vogt
René Vogt

Reputation: 43876

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.

Upvotes: 2

Zein Makki
Zein Makki

Reputation: 30022

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

Upvotes: 0

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

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

Upvotes: 1

Related Questions