我想我一直在解决这个错误或措辞不正确。
我有一个带有LINQ查询的控制器,该控制器返回一个列表,并且其中包含的是每个项目的子列表。我正在使用EPPlus为它创建一个excel文档,到目前为止,它一直有效,直到我想添加到子列表中为止。
我想要做的是为每个行项目提供一个扩展选项,该选项可扩展到每个项目内的子列表。下面是我到目前为止的代码。
控制器(在此示例中,我将其砍掉了,因此,如果只是因为黑客和工作而无法看到它。Results.ToList()实际上是一个相当长的LINQ查询。
[HttpGet]
[Route("Download")]
public FileContentResult Download(int? mid)
{
using (var package = new ExcelPackage())
{
byte[] fileContents;
using (var ctx = new dbEntities())
{
results.ToList();
var merchantIdList = (results.GroupBy(t => t.DomainMerchantID, t => t.violations)
.OrderBy(g => g.Key)
.Select(g => g.Key).ToList());
var resultList = new List<ViolationsByMerchant>();
foreach (int? id in merchantIdList)
{
var thisResult = (from r in results
where r.violations.DomainMerchantID == id
orderby r.violations.DateOfInfraction descending
select new ViolationsByMerchant
{
SellerDomain = r.DomainName,
Merchant = r.IdentifierNiceName,
Contact = r.str_Name,
Phone = r.str_Phone,
AccountMatch = "????",
ViolationsProductDetails = from v in results
where v.violations.DomainMerchantID == id
select new ViolationsProductDetails()
{
ProductName = r.memberProducts.ProductName,
ProductLine = r.memberProducts.ProductLine,
Category = r.memberProducts.ProductCategory
}
}).FirstOrDefault();
resultList.Add(thisResult);
}
var worksheet = package.Workbook.Worksheets.Add("Merchant Violations");
int i = 1;
foreach (var item in resultList)
{
worksheet.Cells["A1:Z1"].Style.Font.Size = 15;
worksheet.Cells.AutoFitColumns();
worksheet.Cells["A1:Z1"].Style.Font.Bold = true;
worksheet.Cells[1, 1].Value = "Merchant";
worksheet.Cells["A" + i].Value = item.Merchant;
worksheet.Cells[1, 2].Value = "Account Match";
worksheet.Cells["B" + i].Value = item.AccountMatch;
worksheet.Cells[1, 3].Value = "Seller";
worksheet.Cells["C" + i].Value = item.SellerDomain;
worksheet.Cells[1, 4].Value = "Contact";
worksheet.Cells["D" + i].Value = item.Contact;
worksheet.Cells[1, 5].Value = "Phone";
worksheet.Cells["E" + i].Value = item.Phone;
i++;
}
fileContents = package.GetAsByteArray();
}
return File(
fileContents: fileContents,
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
fileDownloadName: "MyMappProducts.xlsx"
);
}
}
LINQ如果有帮助,则返回类似这样的内容。
{
"merchant": "Foo Merchant,
"accountMatch": "True",
"sellerDomain": "Amazon",
"contact": "John Doe",
"phone": "555-555-5555
"violationsProductDetails": [
{
"productName": "King Kong",
"productLine": "Animal",
"category": "King of the jungle",
},
{
"productName": "Prime",
"productLine": "Transformers",
"category": "Toy",
},
}