我想在我的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();
不知道你的模式/訪問模式等,如果你改變查詢var banks =(從db.BankListMaster 其中b.Status.Equals(「A」)選擇b).AsEnumerable ().Select(x => new {...})'而不是?否則,您應該能夠將FixedStats/VariableStats設置爲字符串集合,並執行第二次select(在AsEnumerable()'之後)來加入字符串。 – 2013-05-02 16:08:59
性能不是問題,這是一個相對較小的應用程序。我對這種語言很陌生,你能說明一下嗎?我將註釋中的語法複製到'new {...}'行,然後將我在'{...}'部分中的'select new'部分中的所有內容放在我的帖子中。在將'b.'替換爲'x.'後,程序拋出了一個'NullReferenceException' – NealR 2013-05-02 16:16:37
這是在錯誤頁面中單獨出現的代碼行:'.Select(x => new' – NealR 2013-05-02 16:23:31