2

I am trying to make a LINQ statement where the where clause is a string variable. For example:

string whereClause = "(Code = '12') AND (Name = 'a')" ;
var referreds = from r in _db.Referreds where whereClause;

Edit: I have a model with 30 property, i want to created search for this model. I want to select items where user insert value for every property.

For example, where user insert value for Code&Name&Family serach with this propertie, and where user insert value for Gender,Name,Study,Degree serach with this.

which is Better solution for this?

ar.gorgin
  • 4,765
  • 12
  • 61
  • 100

1 Answers1

1

Linq doesn't have this functionality by default. You have to use a third party library to do what you want.

Check out this blog by Scott GU on the subject.

http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

sam starobin
  • 258
  • 3
  • 12
  • Was just about to post the same, used it before however I prefer to dynamically build the query and just use .Where(foo) instead – finman Jul 10 '15 at 13:40
  • If you want dynamically built Where clauses in LINQ, just append them as you go. Remember the query isn't evaluated until it's enumerated. – Spivonious Jul 10 '15 at 13:41