我有一个List<Device>
。在Device
类中有4个属性,即Name
,OperatingSystem
,Status
和LastLoggedInUser
。我需要写一个方法:使用Linq进行动态过滤
IQueryable<Device> FilterDeviceList(
List<Device> Devices,
List<string> filter,
string filterValue)
其中filter
将包含过滤"name"
,"os"
选项来指示字段搜索中包含。如果通过"all"
则需要包含所有4个字段。 filtervalue
将包含要被过滤的值,如"windows"
,"Calvin"
。
任何人都可以提出一种方法来实现这一目标吗?
编辑:
如果我不清楚,我做了过滤有点像这样的,它是我需要的代码的注释部分。
if(filter.contains(name))
{
//filter with name
}
if(filter.contains(both name and os)
{
// I only need the filter value to contain in name or OS (only needed in any one of the field,not necessary to be in both)
}`
if语句你总是可以做到这一点,但我不知道它是否能与动态LINQ来完成。 –