5

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 ?

1
  • Linq queries must be strongly typed in compile time, you can't do it like you do SQL queries. Commented Feb 10, 2015 at 12:22

4 Answers 4

8

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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.

6 Comments

is it possible to add condition to the where clause like a string ? i mean like this context.tblName.Where("some condtion")
No, it needs to be strongly typed. What is your use case exactly?
I mean putting where clause like this context.tblName..Where("SERIAL_NUMBER == xxx && MAC==xxx") i tried but it doesn't accept it
I understood what you meant. But why do you need to have the clause passed as a string?
because my clause came from a form like a filter this is the reason i want to make it like this
|
2

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.

Comments

0

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

2 Comments

Thanks now i am working with this library and trying to find a solution from it.
@Younisbarznji If this has answered your question then please mark it as the answer and close this topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.