下面是我在 ASP MVC 3 控制器中用于导出到.xls
文件的两表 linq 查询。但是,我正在查询的表与辅助表具有一对多的关系。FixedStats
当我单步执行代码时,我可以看到 linq 查询已按应有的方式执行, andVariableStats
字段中的信息量正确。但是,当文件导出到电子表格时,这两列就找不到了。
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();
}