C# Lambda表达式,如Sql,其中包含值或null
我正在编写一个搜索我们正在构建的MVC应用程序的代码,我想通过对象的各种属性进行搜索。在这种情况下,这是我的预期行为:C# Lambda表达式,如Sql,其中包含值或null,c#,.net,linq,C#,.net,Linq,我正在编写一个搜索我们正在构建的MVC应用程序的代码,我想通过对象的各种属性进行搜索。在这种情况下,这是我的预期行为: 如果两个参数都为null或空,则返回all 如果任何参数有值,请使用“包含”选择该参数筛选的所有参数 这就是我正在做的: var model = _svc.GetList(q => q.Name == (string.IsNullOrEmpty(entity.Name) ? q.Name : entity.Name) &&
- 如果两个参数都为null或空,则返回all
- 如果任何参数有值,请使用“包含”选择该参数筛选的所有参数
var model = _svc.GetList(q => q.Name == (string.IsNullOrEmpty(entity.Name) ? q.Name : entity.Name) &&
q.Description == (string.IsNullOrEmpty(entity.Description) ? q.Description : entity.Description));
如果两个字段都为null或空,则返回所有元素,或者返回与名称和/或描述完全匹配的任何元素
这里的问题是,我想让它表现为包含
我已经成功地在一个字段中实现了这一点:
var model = _svc.GetList(q => (string.IsNullOrEmpty(entity.Name) ||
q.Name.ToLower().Contains(entity.Name.ToLower()))).ToList();
但是当我添加Description字段时,它抛出一个NullReferenceException:对象引用未设置为对象的实例
只是想确认一下,我试过这个,但也没用:
var model = (from q in _svc.GetList()
where (string.IsNullOrEmpty(module.Name) ||
q.Name.ToLower().Contains(module.Name.ToLower()))
select q).ToList();
model = (from q in model
where (string.IsNullOrEmpty(module.Description) ||
q.Description.ToLower().Contains(module.Description.ToLower()))
select q).ToList();
这有点难看,但应该做到这一点,因为带有空Description
的条目会导致空引用。如果您对名称所做的操作有任何线索,那么您可能正在执行类似于q.Description.ToLower()包含(…)
的操作,而没有检查q.Description是否为空
var model = _svc.GetList(q =>
(string.IsNullOrEmpty(entity.Name) || string.IsNullOrEmpty(q.Name)
|| q.Name.ToLower().Contains(entity.Name.ToLower()))
&& (string.IsNullOrEmpty(entity. Description) || string.IsNullOrEmpty(q. Description)
|| q.Description.ToLower().Contains(entity. Description.ToLower())))
这有点难看,但应该做到这一点,因为带有空Description
的条目会导致空引用。如果您对名称所做的操作有任何线索,那么您可能正在执行类似于q.Description.ToLower()包含(…)
的操作,而没有检查q.Description是否为空
var model = _svc.GetList(q =>
(string.IsNullOrEmpty(entity.Name) || string.IsNullOrEmpty(q.Name)
|| q.Name.ToLower().Contains(entity.Name.ToLower()))
&& (string.IsNullOrEmpty(entity. Description) || string.IsNullOrEmpty(q. Description)
|| q.Description.ToLower().Contains(entity. Description.ToLower())))
对于多个可选条件搜索,您可以这样做(断言“模块”是您的“搜索类”)
简单,而且(我认为)更具可读性。为模型的描述和实体属性添加空检查
//take all elements
var model = _svc.GetList();
if (!string.IsNullOrEmpty(module.Description))
model = model.Where(m =>
m.Description != null &&
m.Description.ToLower().Contains(module.Description.ToLower());
if (!string.IsNullOrEmpty(module.Name))
model = model.Where(m =>
m.Name != null &&
m.Name.ToLower().Contains(module.Name.ToLower());
return module.ToList();
Null检查,因为Null上的ToLower()
将引发NRE 对于多个可选条件搜索,您可以这样做(断言“模块”是您的“搜索类”)
简单,而且(我认为)更具可读性。为模型的描述和实体属性添加空检查
//take all elements
var model = _svc.GetList();
if (!string.IsNullOrEmpty(module.Description))
model = model.Where(m =>
m.Description != null &&
m.Description.ToLower().Contains(module.Description.ToLower());
if (!string.IsNullOrEmpty(module.Name))
model = model.Where(m =>
m.Name != null &&
m.Name.ToLower().Contains(module.Name.ToLower());
return module.ToList();
Null检查,因为Null上的ToLower()
将引发NRE