如何基于先前返回的值向LINQ查询添加条件条件 [英] How to add conditional condition to LINQ query based on a previously returned value
本文介绍了如何基于先前返回的值向LINQ查询添加条件条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
var sList = from ASL in aslist
where ASL.Description == as.ToString()
select ASL;
as = sList.FirstOrDefault();
var rList = from ARL in arlist
where ARL.Description == rDescription && ARL.Id == as.Id
select ARL;
现在,如您所见,rList
查询的最后条件(即&& ARL.Id == as.Id
)基于从sList
查询中检索到的值.我的问题是当sList
是NULL
时,出现错误.我只想在sList
不为null的情况下包括最后一个条件(即&& ARL.Id == as.Id
).
Now as you see, the last condition of rList
query (i.e. && ARL.Id == as.Id
) is based on the value retrieved from sList
query. My problem is when sList
is NULL
, I get an error. I want to include last condition (i.e. && ARL.Id == as.Id
) only if sList
is not null.
我可以使用如下所示的其他方式来做到这一点,但如果可能的话,我希望使用LINQ来做到这一点:
I can do it using if else like as shown below, but I want to do it elegantly using LINQ if possible:
if (as != null)
{
// include ARL.Id == as.Id condition in the query
}
else
{
// exclude ARL.Id == as.Id condition from the query
}
提前谢谢!
推荐答案
&& (assetsensor == null || ARL.SensorID == assetsensor.ID)
这篇关于如何基于先前返回的值向LINQ查询添加条件条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文