0

我想在我的ASP MVC 3站點中使用實體框架將Linq查詢綁定到GridView數據源。然而,因爲我需要從一個輔助表中提取信息兩個領域的,我得到的錯誤替代在Linq查詢中使用String.Join

LINQ to Entities does not recognize the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable'1[System.String])' method, and this method cannot be translated into a store expression.

我希望能夠做到這一點,而無需創建一個專用的視圖模型。 Linq查詢中是否有使用String.Join的替代方法?

var grid = new System.Web.UI.WebControls.GridView(); 

//join a in db.BankListAgentId on b.ID equals a.BankID 
var banks = from b in db.BankListMaster 
    where b.Status.Equals("A") 
    select new 
    { 
     BankName = b.BankName, 
     EPURL = b.EPURL.Trim(), 
     AssociatedTPMBD = b.AssociatedTPMBD, 
     FixedStats = String.Join("|", from a in db.BankListAgentId 
             where a.BankID == b.ID && 
             a.FixedOrVariable.Equals("F") 
             select a.AgentId.ToString()), 
     VariableStats = String.Join("|", from a in db.BankListAgentId 
             where a.BankID == b.ID && 
             a.FixedOrVariable.Equals("V") 
             select a.AgentId.ToString()), 
     SpecialNotes = b.SpecialNotes, 
    }; 

grid.DataSource = banks.ToList(); 
grid.DataBind(); 
+0

不知道你的模式/訪問模式等,如果你改變查詢var banks =(從db.BankListMaster 其中b.Status.Equals(「A」)選擇b).AsEnumerable ().Select(x => new {...})'而不是?否則,您應該能夠將FixedStats/VariableStats設置爲字符串集合,並執行第二次select(在AsEnumerable()'之後)來加入字符串。 – 2013-05-02 16:08:59

+0

性能不是問題,這是一個相對較小的應用程序。我對這種語言很陌生,你能說明一下嗎?我將註釋中的語法複製到'new {...}'行,然後將我在'{...}'部分中的'select new'部分中的所有內容放在我的帖子中。在將'b.'替換爲'x.'後,程序拋出了一個'NullReferenceException' – NealR 2013-05-02 16:16:37

+0

這是在錯誤頁面中單獨出現的代碼行:'.Select(x => new' – NealR 2013-05-02 16:23:31

回答

1

如果你不是太擔心性能(因爲它有子查詢,則可能產生N + 1個查詢數據庫,如果數據庫行很大,你可以獲取非必要的數據),最簡單的解決方法是添加一個AsEnumerable()以在Web /應用程序端執行String.Join;

var banks = (from b in db.BankListMaster 
      where b.Status.Equals("A") select b) 
      .AsEnumerable() 
      .Select(x => new {...}) 

在通話的點AsEnumerable(),LINQ查詢的其餘部分將在應用端,而不是數據庫端進行,所以你可以自由地使用任何運營商,你需要得到這份工作完成。當然,在此之前,你會想盡可能地過濾結果。