【发布时间】:2014-02-10 11:11:03
【问题描述】:
我有一个List<Device>。在Device类中有4个属性,分别是Name、OperatingSystem、Status和LastLoggedInUser。我需要写一个方法:
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 做到。