例如:
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
不为空时,我想包括最后一个条件(即 && ARL.Id == as.Id
)。
我可以使用 if else 来完成它,如下所示,但我想尽可能使用 LINQ 优雅地完成它:
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)
关于c# - 如何根据先前返回的值向 LINQ 查询添加条件条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16347498/