LINQ中的动态查询 [英] Dynamic query in LINQ

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

问题描述

如果我说包含该字段的Customer类,该如何为Linq编写动态查询:

How do I write a dynamic query for Linq, if I have say Customer class which holds the fields:

string name
string address
int phoneno

我必须根据类似的信息进行查询

I have to query based on information given similar to

query = string.Empty;

if(!string.IsNullorEmpty(name))
{
   query += "@name = name";
}

if(!string.IsNullorEmpty(address))
{
   query += "@address = address";
}

if(!string.IsNullorEmpty(phoneno))
{
   query += "@phoneno = phoneno";
}

var result = from condition in customer
    where(query)
    select condition;

编辑#1:

项目可以在运行时更改,例如

the items are changeable at run time like

private Customer[] GetCustomers(Dictionary<string,string> attributes)
{
   here the attribute may be, name alone, or name and address, or name address and phoneno


      foreach(string field in attributes.key)
      {
           query += field == attributes[key];

      }

         Customers[] =ExecuteQuery(query);

}

LINQ支持这种查询吗?

Is this kind of query supported by LINQ?

编辑#2:

Mouk
由于我是C#的新手,所以我仍在努力,这对我不起作用.

Hi Mouk,
As I am new to C#, I am still struggling, this is not working for me.

var query = _ConfigFile.ConnectionMasterSection;

for(int i = 0; i < filter.count; i++)
{
    query = result.Where(p => typeof(ConnectionMaster).GetProperty(filter[i].Attribute).Name == filter[i].Value);
}

这是空的,就像我在这里使用的

This yeilds Empty, where as i used this

var query = _ConfigFile.ConnectionMasterSection;

//Hard coded
res.Where(q => q.category == filter[0].Value);

它按我的预期工作.

布莱恩·瓦茨(Bryan Watts),
我也尝试了您的代码,但收到以下错误消息:"Lambda参数不在范围内".

Hi Bryan Watts,
I tried your code also and I getting this error: "Lambda Parameter not in scope".

for(int i = 0; i < filter.count; i++)
{
    Field item = filter[i];

    MemberExpression param = Expression.MakeMemberAccess(Expression.Parameter(typeof(Connection), "p"), typeof(Connection).GetProperty(item.Attribute));

    MemberExpression constant = Expression.MakeMemberAccess(Expression.Constant(item), typeof(Field).GetProperty("Value"));
}


try
{
    var myquery = Queryable.Where(coll, Expression.Lambda<Func<Connection, bool>>(
    Expression.Equal(param, constant), Expression.Parameter(typeof(Connection),"p")));
}

这是什么错误?

推荐答案

查看以下 http: //www.albahari.com/nutshell/predicatebuilder.aspx ,它允许进行强类型的谓词构建,这确实非常好.如果您实际上希望使用动态字符串构建谓词,则可以使用

Check out this http://www.albahari.com/nutshell/predicatebuilder.aspx, it allows for strongly typed predicate building, it can be really nice. If you want actually dynamic string built predicates than you can use the LINQ Dynamic Query Library provided by ScottGu.

尽管我会建议第二种选择之前的第一种选择,但两者都能完成您想要的.

Both will accomplish what you want although I would recommend the first option before the second.

允许您这样做:

var predicate = PredicateBuilder.True<MyLinqType>();

if(!string.IsNullOrEmpty(name))
    predicate = predicate.And(p => p.name == name);


...

var myResults = Context.MyLinTypeQueryTable.Where(predicate);

还有更多

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

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