Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create linq query as string

Tags:

string

c#

linq

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 ?

like image 720
Younis Qadir Avatar asked Feb 10 '15 12:02

Younis Qadir


3 Answers

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)

like image 143
Mark Avatar answered Sep 21 '22 00:09

Mark


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.

like image 45
Carles Company Avatar answered Sep 19 '22 00:09

Carles Company


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.

like image 21
Rob Avatar answered Sep 19 '22 00:09

Rob