c#
  • linq
  • entity
  • 2011-05-09 157 views 1 likes 
    1

    我需要建立在客戶表的搜索查詢與訂單表連接搜索查詢3.5

    string firstname = "Joe"; 
    string emailFilter = "[email protected]"; 
    string city=null; 
    

    在SQL我們可以作出這樣的

    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' 
    

    我需要建立一個實體框架3.5 linq查詢加入訂單和客戶表 與動態搜索條件。

    如果值不爲空,我需要在LINQ

    條款我是新來的LINQ在何處使用。 我們需要使用Iqueryable。 任何幫助表示讚賞。

    感謝

    +0

    請在提出問題之前編譯您的代碼並查看錯誤消息 – jeroenh 2011-05-09 10:51:32

    回答

    1

    你可以嘗試這樣的:

    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; 
    
    1

    你可以在這裏查看動態的LINQ http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx它會幫助你或搜索stackoverflow.com爲動態LINQ標籤的問題和答案https://stackoverflow.com/questions/tagged/dynamic-linq

    相關問題