Younis Qadir
Younis Qadir

Reputation: 435

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

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

and here is my linq query string :

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 ?

Upvotes: 5

Views: 19733

Answers (4)

Rob
Rob

Reputation: 11798

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. errorsOnly, startDate and endDate can (but must not) be set in the frontend. 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.

Upvotes: 2

Mark
Mark

Reputation: 3283

What you're looking for is something like 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)

Upvotes: 8

JunaidKirkire
JunaidKirkire

Reputation: 918

I think what you are looking for is Dynamic LINQ. This is a library provided by the LINQ team itself.

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

Upvotes: 0

Carles Company
Carles Company

Reputation: 7216

Maybe you'll have more luck using the linq static methods:

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

This way is really easy to add where clauses (or other) dynamically:

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.

Upvotes: 2

Related Questions