C# 使用lambda表达式对IEnumerable-List进行多个可空条件的筛选
我使用.NET4.5和VS2012 我有一个类模型(来自Xml反序列化),现在我有了“过滤”它 我有几个条件的可空值 使用lambda表达式的最佳实践是什么 我有以下代码:C# 使用lambda表达式对IEnumerable-List进行多个可空条件的筛选,c#,linq,lambda,where-clause,conditional-statements,C#,Linq,Lambda,Where Clause,Conditional Statements,我使用.NET4.5和VS2012 我有一个类模型(来自Xml反序列化),现在我有了“过滤”它 我有几个条件的可空值 使用lambda表达式的最佳实践是什么 我有以下代码: public static List<MyDto> ListarEntornosYContenidoEntorno(int? id, string name, int? id2, string name2, string entorno, bool? shouldBe, string mode)
public static List<MyDto> ListarEntornosYContenidoEntorno(int? id, string name, int? id2, string name2, string entorno, bool? shouldBe, string mode)
{
IEnumerable<MyDto> list = Model.Environments;
if (id.HasValue)
list = list.Where(item => item.IdCiaDespliegue.Equals(id.Value));
if (!name.IsNullOrWhiteSpace())
list = list.Where(item => item.NombreCiaDespliegue.Equals(name));
if (id2.HasValue)
list = list.Where(item => item.IdContenidoEntorno.Equals(id2.Value));
if (!name2.IsNullOrWhiteSpace())
list = list.Where(item => item.ContenidoEntorno.Equals(name2));
if (!entorno.IsNullOrWhiteSpace())
list = list.Where(item => item.Entorno.Equals(entorno));
if (shouldBe.HasValue)
list = list.Where(item => item.DebeEtiquetar.Equals(shouldBe));
if (!mode.IsNullOrWhiteSpace())
list = list.Where(item => item.Modo.Equals(mode));
return list.ToList();
}
public static List arentornosycontenidontorno(int?id、字符串名称、int?id2、字符串名称2、字符串entorno、bool?shouldBe、字符串模式)
{
IEnumerable list=Model.Environments;
if(id.HasValue)
list=list.Where(item=>item.IdCiaDespliegue.Equals(id.Value));
如果(!name.IsNullOrWhiteSpace())
list=list.Where(item=>item.NombreCiaDespliegue.Equals(name));
if(id2.HasValue)
list=list.Where(item=>item.IdContenidoEntorno.Equals(id2.Value));
如果(!name2.IsNullOrWhiteSpace())
list=list.Where(item=>item.ContenidoEntorno.Equals(name2));
如果(!entorno.IsNullOrWhiteSpace())
list=list.Where(item=>item.Entorno.Equals(Entorno));
if(shouldBe.HasValue)
list=list.Where(item=>item.DebeEtiquetar.Equals(shouldBe));
如果(!mode.IsNullOrWhiteSpace())
list=list.Where(item=>item.Modo.Equals(mode));
return list.ToList();
}
您可以执行以下操作(不确定是否可读):
IEnumerable list=Model.Environments;
返回列表。其中(item=>!id.HasValue | | item.IdCiaDespliegue==id.Value)
.Where(item=>string.IsNullOrWhiteSpace(name)| | item.NombreCiaDespliegue==name)
.Where(item=>!id2.HasValue | | item.IdContenidoEntorno==id2.Value)
.Where(item=>string.IsNullOrWhiteSpace(name2)| | item.ContenidoEntorno==name2)
.Where(item=>string.IsNullOrWhiteSpace(entorno)| | item.entorno==entorno)
.Where(item=>!shouldBe.HasValue | | item.DebeEtiquetar==shouldBe)
.Where(item=>string.IsNullOrWhiteSpace(mode)| | item.Modo==mode)
.ToList();
诀窍就是在选择器为null或空时返回true
,这样就不会过滤出任何项目
请注意,我将所有的.Equals
替换为=
,因为在本例中,它们是等效的,=
在我看来更可读。您可以执行以下操作(不确定是否更可读):
IEnumerable list=Model.Environments;
返回列表。其中(item=>!id.HasValue | | item.IdCiaDespliegue==id.Value)
.Where(item=>string.IsNullOrWhiteSpace(name)| | item.NombreCiaDespliegue==name)
.Where(item=>!id2.HasValue | | item.IdContenidoEntorno==id2.Value)
.Where(item=>string.IsNullOrWhiteSpace(name2)| | item.ContenidoEntorno==name2)
.Where(item=>string.IsNullOrWhiteSpace(entorno)| | item.entorno==entorno)
.Where(item=>!shouldBe.HasValue | | item.DebeEtiquetar==shouldBe)
.Where(item=>string.IsNullOrWhiteSpace(mode)| | item.Modo==mode)
.ToList();
诀窍就是在选择器为null或空时返回true
,这样就不会过滤出任何项目
请注意,我将所有的.Equals
替换为=
,因为在本例中,它们是等效的,=
在我看来更可读。您可以执行以下操作(不确定是否更可读):
IEnumerable list=Model.Environments;
返回列表。其中(item=>!id.HasValue | | item.IdCiaDespliegue==id.Value)
.Where(item=>string.IsNullOrWhiteSpace(name)| | item.NombreCiaDespliegue==name)
.Where(item=>!id2.HasValue | | item.IdContenidoEntorno==id2.Value)
.Where(item=>string.IsNullOrWhiteSpace(name2)| | item.ContenidoEntorno==name2)
.Where(item=>string.IsNullOrWhiteSpace(entorno)| | item.entorno==entorno)
.Where(item=>!shouldBe.HasValue | | item.DebeEtiquetar==shouldBe)
.Where(item=>string.IsNullOrWhiteSpace(mode)| | item.Modo==mode)
.ToList();
诀窍就是在选择器为null或空时返回true
,这样就不会过滤出任何项目
请注意,我将所有的.Equals
替换为=
,因为在本例中,它们是等效的,=
在我看来更可读。您可以执行以下操作(不确定是否更可读):
IEnumerable list=Model.Environments;
返回列表。其中(item=>!id.HasValue | | item.IdCiaDespliegue==id.Value)
.Where(item=>string.IsNullOrWhiteSpace(name)| | item.NombreCiaDespliegue==name)
.Where(item=>!id2.HasValue | | item.IdContenidoEntorno==id2.Value)
.Where(item=>string.IsNullOrWhiteSpace(name2)| | item.ContenidoEntorno==name2)
.Where(item=>string.IsNullOrWhiteSpace(entorno)| | item.entorno==entorno)
.Where(item=>!shouldBe.HasValue | | item.DebeEtiquetar==shouldBe)
.Where(item=>string.IsNullOrWhiteSpace(mode)| | item.Modo==mode)
.ToList();
诀窍就是在选择器为null或空时返回true
,这样就不会过滤出任何项目
请注意,我将所有的
.Equals
替换为=
,因为在本例中,它们是等效的,=
在我看来更具可读性。我已经为您提供了一种方法,可以在下面对可为空的整数进行概括;我相信你可以对弦做类似的处理。此外,您应该考虑更改API,以便此方法接受“过滤器”列表。
IEnumerable<MyDto> list = Model.Environments;
return list.Where(item => !id.HasValue || item.IdCiaDespliegue == id.Value)
.Where(item => string.IsNullOrWhiteSpace(name) || item.NombreCiaDespliegue == name)
.Where(item => !id2.HasValue || item.IdContenidoEntorno == id2.Value)
.Where(item => string.IsNullOrWhiteSpace(name2) || item.ContenidoEntorno == name2)
.Where(item => string.IsNullOrWhiteSpace(entorno) || item.Entorno == entorno)
.Where(item => !shouldBe.HasValue || item.DebeEtiquetar == shouldBe)
.Where(item => string.IsNullOrWhiteSpace(mode) || item.Modo == mode)
.ToList();
var intMap = new Dictionary<int?, Func<Environment, int>>
{
{ id, item => item.IdCiaDespliegue },
{ id2, item => item.IdContenidoEntorno },
{ entorno, item => item.DebeEtiquetar }
}
var list = Model.Environments;
foreach(var pair in intMap)
{
if(pair.Key != null)
list = list.Where(item => pair.Value(item).Equals(pair.Key.Value));
}