2016-07-08 52 views
-2

我有一个列表Vehicles与一些字符串属性。 我有另一个列表filterList用户选择他想要的属性。 在filterValues应该是用户选择的属性的值。 我该怎么做?Linq从列表中选择一个属性

到目前为止,我在想是这样的:

var filterValues = =new List<string>(); 
foreach(var filter in filterList) 
{ 
    filterValues = vehicles.Select(x=>x. + filter).ToList(); 
} 

但毕竟这项目从filterValues包含x.filter。 谢谢!

+0

是'FilterValues'应该是要选择有属性的列表?请显示'vehicles'元素类型的定义。 – Codor

+0

如果您想要深入查询您正在查询的类的属性,请使用SelectMany()。 https://msdn.microsoft.com/en-us/library/bb534336(v=vs.100).aspx – Fredrik

+0

FilterValues假设具有该属性的所有值。 – RobertRPM

回答

1

我想这将是你的汽车类

public class Vehicle 
{ 
    public string Foo {set; get;} 
    public string Bar {set; get;} 
} 


var filterValues = new List<string>(); 
vehicles.Select(x => filterValues.Any(f => f.Contains(x.Foo) || f.Contains(x.Bar)) 
        .ToList(); 
+0

需要位于filterValues中的属性由用户介绍。 – RobertRPM

+0

@RobertRPM这将是没有问题,并与此解决方案一起工作,只需在声明后添加到filterValues – Kahbazi

+0

'filterValues.Any(f => f.Contains(x.Foo)|| f.Contains(x.Bar))' – Sherlock