我有一個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來完成。 –