且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在linq where子句中使用字符串变量?

更新时间:2022-12-18 11:29:47

Linq默认情况下不具有此功能.您必须使用第三方库来做您想做的事.

请查看Scott GU撰写的有关此主题的博客.

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

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?

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