I have a List<Device>. In the Device class there are 4 properties, namely Name, OperatingSystem, Status and LastLoggedInUser. I need to write a method:
IQueryable<Device> FilterDeviceList(
List<Device> Devices,
List<string> filter,
string filterValue)
where filter will contain options for filtering "name", "os" to indicate the fields to include in the search. If "all" is passed then all 4 fields need to be included.
filtervalue will contain the value to be filtered like "windows", "Calvin".
Can anyone suggest a method to achieve this?
Edit:
If I was not clear, I am doing the filtering somewhat like this, it is the commented part for which I need the code.
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)
}`
You could build your query as follows:
private static IQueryable<Device> FilterDeviceList(List<Device> devices, Device device)
{
var query = devices.AsQueryable();
if (device.Name != null)
query = query.Where(d => d.Name == device.Name);
if (device.OS != null)
query = query.Where(d => d.OS == device.OS);
if (device.Status != null)
query = query.Where(d => d.Status == device.Status);
if (device.LastLoggedInUser != null)
query = query.Where(d => d.LastLoggedInUser == device.LastLoggedInUser);
return query;
}
Then you can call this function with a device object. I.e. if you want name to be included, just pass a device object with a name (leave other properties to their default value). If you want everything to be included, pass in a device object with everything filled in:
var r = FilterDeviceList(devices, new Device
{
Name = "yourFilterValue",
OS = "yourFilterValue",
LastLoggedInUser = "yourFilterValue",
Status = "yourFilterValue"
});
Edit, filter on name property:
var r = FilterDeviceList(devices, new Device
{
Name = "yourFilterValue"
});
Edit 2, take a look at predicatebuilder
var predicate = PredicateBuilder.False<Device>();
if(document.Name != null)
predicate = predicate.Or(d => d.Name == document.Name);
if(document.OS != null)
predicate = predicate.Or(d => d.OS == document.OS);
return devices.Where(predicate);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With