动态Linq查询 [英] Dynamic Linq Query

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

问题描述

我有一个返回List< Dictionary< string,object>>的函数。



此列表的示例如下所示



{("Source","srchostname1"),("Destination","destination"),("State", ),("Created",01/02/2010 12:32:20 pm),("Source","srchostname2"),("Destination","destination2"),("State", "错误"),("创建",02/02/2010 12:32:20 pm),("Source","srchostname3"),
("Destination","destination3"和),("状态","错误"),("创建",04/02/2010 12:32:20 pm)}




此列表将包含-1000个此类条目。



我希望能够根据"来源"进行过滤。 "目的地" "状态" "创建" ;.此过滤器基于用户的选择。它可以是上述任何一种的组合。我也希望能够通过以上任何一种方式进行订购。我也想要
能够"排序"根据用户的选择升序或降序。我还想在结果集上使用Paging。



如何使用动态linq查询来实现上述目标。我正在谷歌搜索,我看到一些PredicateBuilder类可用于过滤器(where子句)  part。谁能帮助我如何使用PredicateBuilder类或任何其他方式实现上述目的。

解决方案

//要查询的数据

List< Dictionary< string,object>> listOfStuff = new List< Dictionary< string,object>>()

{  新词典< string,object> {{" Source"," srchostname1"},{" Destination"," destination"},{" State"," Transferring"},{" Created",DateTime.Parse(" 01 / 02/2010 12:32:20 pm")}},

   新词典< string,object> {{" Source"," srchostname2"},{" Destination"," destination2"},{" State"," Error"},{" Created",DateTime.Parse(" 02) / 02/2010 12:32:20 pm")}},

   新词典< string,object> {{" Source"," srchostname3"},{" Destination"," destination3"},{" State"," Error"},{" Created",DateTime.Parse(" 04) / 02/2010 12:32:20 pm")}}

};


//要应用的过滤器:名称和值...

List< Tuple< string,object>> filters = new List< Tuple< string,object>>()

{

    new Tuple< string,object>(" Source"," srchostname1"),

    new Tuple< string,object>(" State"," Transferring")

};


// sort order arguments:field name,ordinal(在sort args)中,升序(false)或降序(true)

List< Tuple< string,int,bool>> sortArguments = new List< Tuple< string,int,bool>>

{

    new Tuple< string,int,bool>(" Source",1,false),

    new Tuple< string,int,bool>(" Destination",2,true)

};


//将列表转换为查询

var query = listOfStuff.AsQueryable();
$


//应用过滤器

foreach(元组< string,object> ;过滤器过滤器)

{

    string fieldName = filter.Item1;

    object fieldValue = filter.Item2;

    query = query.Where(d => d.Any(v => v.Key == fieldName&& v.Value == fieldValue));

}


//应用排序参数(按降序排列)

foreach(sortArguments.OrderByDescending(o => o.Item2)中的元组< string,int,bool> sortArg)

{

     string orderByCol = sortArg.Item1;

     bool  descendingOrder = sortArg.Item3;



    if(descendingOrder == false)

    {

        query = query.OrderBy(o => o [orderByCol]);

    }
   否则为
    {

        query = query.OrderByDescending(o => o [orderByCol]);

    }
}


//执行查询并将结果转换为列表

var result = query.ToList();


I have a function which returns a List<Dictionary<string, object>>.

An example of this list would look like this

{ ("Source", "srchostname1"), ("Destination", "destination"), ("State","Transferring"), ("Created",01/02/2010 12:32:20pm) , ("Source", "srchostname2"), ("Destination", "destination2"), ("State","Error"), ("Created",02/02/2010 12:32:20pm), ("Source", "srchostname3"), ("Destination", "destination3"), ("State","Error"), ("Created",04/02/2010 12:32:20pm) }

This list would contains -1000- of such entries.

I would like to be able filter based on "Source" "Destination" "State" "Created". This filter is based on user's choice. It can be a combination of any of the above. I would also like to be able to orderby any of the above. I would also like to be able to "Sort" Ascending or descending based on user's choice. I would also like to use Paging on the result set.

How to use a dynamic linq query to achieve the above. I'm googling and i see some PredicateBuilder class available for the filter (where clause) part. Can anyone help me on how i can achieve the above using PredicateBuilder class or any other way.

解决方案

//data to be queried
List<Dictionary<string, object>> listOfStuff = new List<Dictionary<string, object>>()
{   new Dictionary<string,object> { {"Source", "srchostname1"}, {"Destination", "destination"}, {"State","Transferring"}, {"Created", DateTime.Parse("01/02/2010 12:32:20pm") } },
    new Dictionary<string,object> { {"Source", "srchostname2"}, {"Destination", "destination2"}, {"State","Error"}, {"Created", DateTime.Parse("02/02/2010 12:32:20pm")}},
    new Dictionary<string,object> { {"Source", "srchostname3"}, {"Destination", "destination3"}, {"State","Error"}, {"Created", DateTime.Parse("04/02/2010 12:32:20pm")}}
};

// filters to be applied: name and value...
List<Tuple<string, object>> filters = new List<Tuple<string, object>>()
{
    new Tuple<string, object>("Source", "srchostname1"),
    new Tuple<string, object>("State", "Transferring")
};

//sort order arguments: field name, ordinal (in sort args), ascending (false) or descending (true)
List<Tuple<string, int, bool>> sortArguments = new List<Tuple<string, int, bool>>
{
    new Tuple<string, int, bool>("Source", 1, false),
    new Tuple<string, int, bool>("Destination", 2, true)
};

//turn the list into a query
var query = listOfStuff.AsQueryable();

//apply filters
foreach (Tuple<string, object> filter in filters)
{
    string fieldName = filter.Item1;
    object fieldValue = filter.Item2;
    query = query.Where(d => d.Any(v => v.Key == fieldName && v.Value == fieldValue));
}

// apply sort arguments (in descending order)
foreach (Tuple<string, int, bool> sortArg in sortArguments.OrderByDescending(o => o.Item2))
{
    string orderByCol = sortArg.Item1;
    bool descendingOrder = sortArg.Item3;

    if (descendingOrder == false)
    {
        query = query.OrderBy(o => o[orderByCol]);
    }
    else
    {
        query = query.OrderByDescending(o => o[orderByCol]);
    }
}

//execute the query and turn results into a list
var result = query.ToList();


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

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