c# - 如何根据先前返回的值向 LINQ 查询添加条件条件

标签 c# .net 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 查询。我的问题是当 sListNULL 时,我得到一个错误。仅当 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/

相关文章:

c# - 如何通过在XmlDocument或XDocument中传递xElement名称来获取绝对xpath?

.net - VS2017 15.3 解决方案文件中的新 GlobalSection 是什么?

c# - 如何使用 Dynamic LINQ 使用 GroupBy

linq - 如何检查 LINQ 中服务器端集合中的列表中是否缺少任何值? (给出 SQL)

c# - 使用 LINQ 获取一个 List<> 中的项目,这些项目位于另一个 List<> 中

c# - 如何使用c#向chrome cookies文件中插入一条新记录

c# - C#中的字符串不变性

c# - 如何使用c#客户端获取azure token ?

c# - GetColumnName() LinqToExcel for csv 文件抛出错误 Microsoft Jet 数据库引擎找不到对象 'Sheet1$.txt'

c# - DataTable 上的 UNIQUE 约束