[英]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)
也许有人认为这更具可读性,但这是一个品味问题。
如果isLatest
为false
或 (如果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.