markzzz
markzzz

Reputation: 47945

Is it possible to use a string for a LINQ query expression?

I need to extract some records if some variables have some values.

For example, if status>0 I need to filter result like :

where object.id=status

else, if status=0, I need to remove this where clauses and return all elements. I'll get rid about :

if(status>0)
   do a linq query with the where clauses
else
   do a link query with that where clauses

too much code, because the variables to check could be more than 4-5.

Is it possible to "inject" a sort of string on LINQ? (So i can create my string and pass it to the LINQ).

I mean somethings like :

string myQuery="";
if(status>0)
   myQuery="where object.id=status";
else
   myQuery="";

is it possible? (Classic mysql behaviour).

Upvotes: 7

Views: 3147

Answers (9)

Vasea
Vasea

Reputation: 5333

You can write the following

IQueryable<T> query = dbContext.SomeObjectSet;

if (condition1) {
    query = query.Where(...)
}

if (condition2) {
    query = query.Where(...)
}

However, you you want to query all entities, you can filter in memory afterwards using LINQ to SQL

Upvotes: 1

Bobby Borszich
Bobby Borszich

Reputation: 11767

Are you attempting to do a conditional LINQ query? if so maybe this would help

var nums = new List<int>() { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
bool getOdd = true;

var query = nums.AsQueryable();

if (getOdd) {
    query = query.Where(i => i == 1 || i == 3);
} else {
    query = query.Where(i => i == 2 || i == 4);
}

var result = query.ToList();

Upvotes: 1

a1ex07
a1ex07

Reputation: 37354

Another option: query.Where(x=>(status>0? x.id==status : 1==1))

Upvotes: 1

Vinay
Vinay

Reputation: 1064

AFAIK, you will need to have 2 different queries.

if(status > 0)
{
    var myquery = From ....
    where object.id = status
}
else
{
    var myquery = From ..
}

Upvotes: 1

sll
sll

Reputation: 62494

It is possible using Dynamic LINQ, see ScottGu's blog post: Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

enter image description here

Upvotes: 3

Chris Snowden
Chris Snowden

Reputation: 5002

You can build up a query like this:

IEnumerable<MyEntity> results = MyEntityContext.MyEntities;
if (status > 0)
    results = results.Where(e => e.id == status);

Does that help?

Upvotes: 3

GvS
GvS

Reputation: 52518

You could do it like this:

  var query = database.MyTable.Where(/* where for all records */);

  if (status > 0) {
      query = query.Where(o => o.id == status);
  }

Linq (to sql and EF) is smart enough to merge the where conditions and send one SQL statement to the database.

Upvotes: 2

Stuart
Stuart

Reputation: 66882

It is possible using dynamic linq - see How to create LINQ Query from string?

My answer there links to Scott Gu's posts and the sample code from Microsoft - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Upvotes: 1

hammar
hammar

Reputation: 139840

Since LINQ is lazy, you can just do

var query = ...

if (status > 0)
{
    query = query.Where(o => o.id == status);
}

Upvotes: 9

Related Questions