0
下面是我在ASP MVC 3控制器中用於導出到.xls
文件的兩表LINQ查詢。然而,我查詢的表與輔助表具有一對多的關係。當我遍歷代碼時,我可以看到linq查詢已經執行了,並且在FixedStats
和VariableStats
字段中輸入了正確的信息量。但是,當文件導出到spreasheet時,無法找到這兩列。ASP MVC 3 - 將多表LINQ查詢綁定到gridview並導出到.xls
public void ExportToCsv()
{
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,
Tier = b.Tier,
FixedStats = from a in db.BankListAgentId
where a.BankID == b.ID &&
a.FixedOrVariable.Equals("F")
select new { a.AgentId },
VariableStats = from a in db.BankListAgentId
where a.BankID == b.ID &&
a.FixedOrVariable.Equals("V")
select new { a.AgentId },
Attachment = b.Attachment,
Status = b.Status
};
grid.DataSource = banks.ToList();
grid.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=BankList.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
你剛纔打我給它。簡化關係將是必要的。另外,我會建議OP創建一個模型來轉儲行,這樣你就不會處理匿名類型 - 它們是陰暗和骯髒的東西:) –
不幸的是,當我用這種語法運行查詢時,出現以下錯誤:'LINQ to Entities does not recognized the method'System.String Join(System.String,System.Collections.Generic.IEnumerable'1 [System.String])''method,this method can not be translated into a store expression.' – NealR
@NealR:這就是爲什麼你應該創建專門的視圖模型,這就是我寫關於ORM <-> SQL轉換問題的意思。 – LukLed