search query in entity framework 3.5
I need to build a search query on Customers table join with orders table
string firstname = "Joe";
string emailFilter = "joe@email.com";
string city=null;
In SQL we can make 开发者_开发技巧like this
SELECT @sql =
'SELECT * from
FROM dbo.Orders o
inner join
JOIN dbo.Customers c ON o.CustomerID = c.CustomerID
WHERE 1 = 1'
IF @firstname IS NOT NULL
SELECT @sql = @sql + ' AND c.firstname= @firstname'
IF @city IS NOT NULL
SELECT @sql = @sql + ' AND c.city >= @city'
I need to build a entity framework 3.5 linq query joined with orders and customers table with dynamic search condition.
if the values are not null, i need to use in where clause in linq
I am new to Linq. shall we need to use Iqueryable. Any help appreciated.
Thanks
You can try something like :
var result = from o in context.Orders.include("customers")
where o.city == (city == null ? o.city : city) && o.firstname == (firstname == null ? o.firstname : firstname)
select o;
You can check Dynamic Linq here http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx it will help you or search stackoverflow.com for dynamic-linq tag questions and answers https://stackoverflow.com/questions/tagged/dynamic-linq
精彩评论