使用Linq进行动态过滤 [英] Dynamic filtering using Linq

查看:72
本文介绍了使用Linq进行动态过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List<Device>.在Device类中,有4个属性,即NameOperatingSystemStatusLastLoggedInUser.我需要写一个方法:

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)

其中filter将包含用于过滤"name""os"的选项,以指示要包括在搜索中的字段.如果通过了"all",则必须包括所有4个字段. filtervalue将包含要过滤的值,例如"windows""Calvin".

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?

如果我不清楚,我正在做这样的过滤,这是我需要代码的注释部分.

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;
}

然后,您可以使用设备对象调用此函数. IE.如果要包含名称,只需传递带有名称的设备对象(将其他属性保留为其默认值).如果要包含所有内容,请传入一个设备对象,并填写所有内容:

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"
            });

编辑,过滤名称属性:

var r = FilterDeviceList(devices, new Device
                {
                    Name = "yourFilterValue"
                });

编辑2,看看 predicatebuilder

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);

这篇关于使用Linq进行动态过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆