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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…