简体   繁体   English

将 linq 查询创建为字符串

[英]Create linq query as string

I have a string that contains a linq query and i have a dynamic where clause also as string that contains many dynamic condition here is my where clause我有一个包含 linq 查询的字符串,我有一个动态 where 子句,也作为包含许多动态条件的字符串,这里是我的 where 子句

string strWhereString = "where a.id==1 && a.name==\"something\"";

and here is my linq query string :这是我的 linq 查询字符串:

var query = "from a in context.tblName "+strWhereString;

the question is how to run this query and get result from the table?问题是如何运行此查询并从表中获取结果? Is there any way to do that or Linq doesn't support this ?有没有办法做到这一点,或者 Linq 不支持这个?

What you're looking for is something like System.Linq.Dynamic你正在寻找的是类似System.Linq.Dynamic

which will give you the possibility to translate a query like:这将使您可以翻译如下查询:

var query = from p in northwind.Products
                where p.CategoryID == 3 && p.UnitPrice > 3
                orderby p.SupplierID
                select p;

into:进入:

var query = northwind.Products
                         .Where("CategoryID = 3 AND UnitPrice > 3")
                         .OrderBy("SupplierID");

also here is a good starting point, which has a good blog post and some examples to download.这里也是一个很好的起点,其中有一篇很好的博客文章和一些可供下载的示例。

Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library) 动态 LINQ(第 1 部分:使用 LINQ 动态查询库)

Maybe you'll have more luck using the linq static methods:也许使用 linq 静态方法会更幸运:

context.tblName.Where(a=>a.id==1 && a.name=="something")

This way is really easy to add where clauses (or other) dynamically:这种方式很容易动态添加 where 子句(或其他):

context.tblName..Where(a=>a.id==1 && a.name=="something").Where(a=>otherClause(a))

I'm not sure if this is really what you're looking for, but I think this is the right direction.我不确定这是否真的是你要找的,但我认为这是正确的方向。

I also had to deal with dynamic conditions for doing a DB search.我还必须处理进行数据库搜索的动态条件。 Instead of string parsing or dynamic LINQ, I came up with this solution.我想出了这个解决方案,而不是字符串解析或动态 LINQ。 errorsOnly , startDate and endDate can (but must not) be set in the frontend. errorsOnlystartDateendDate可以(但不能)在前端设置。 Additional conditions can simply be added accordingly:可以简单地相应地添加附加条件:

var query = from x in db.DoubleDataValueArchive select x;
query = query.Where(x => x.DataPointId != null);

// Check if only errors should be shown (that are listed in errorDps)
List<int> errorDps = new List<int>();
if (errorsOnly.HasValue) {
    if (errorsOnly == true)
    {
        errorDps = db.DataPoints.Where(x => x.DataType == 4).Select(x => x.Id).ToList();
        query = query.Where(x => errorDps.Contains((int)x.DataPointId));
    }
}

// Start Date
if (startDate.HasValue) {
    startDate = startDate.Value.ToUniversalTime();
    query = query.Where(x => x.DateValue >= startDate);
}

// End Date
if (endDate.HasValue)
{
    endDate = endDate.Value.ToUniversalTime();
    query = query.Where(x => x.DateValue <= endDate);
}

...and so on. ...等等。 This is completely dynamic but safe to work with at the same time.这是完全动态的,但同时使用是安全的。 The assembled SQL query only gets finally executed once, when you make a list or similar out of the IQueryable .当您从IQueryable创建列表或类似内容时,组装的 SQL 查询只会最终执行一次。

I think what you are looking for is Dynamic LINQ.我认为您正在寻找的是动态 LINQ。 This is a library provided by the LINQ team itself.这是一个由 LINQ 团队自己提供的库。

What you need to do is use string expressions instead as shown in this blog - http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library您需要做的是使用字符串表达式,如本博客所示 - http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM