[英]Anonymous Type Error in Linq using Group, Sum, and selecting multiple fields of data after reading in spreadsheet with LinqToExcel
I'm reading in a spreadsheet using LinqToExcel, this part seems to work. 我正在使用LinqToExcel在电子表格中阅读,这部分似乎有效。 The issue is that my spreadsheet has multiple rows with the same phone number, i need to group and sum the rows with the same phone number then export each unique row (with it's sum) to SQL.
问题是我的电子表格有多个具有相同电话号码的行,我需要对具有相同电话号码的行进行分组和求和,然后将每个唯一的行(及其总和)导出到SQL。 I'm stuck on the Linq / Lambda statements, I keep getting an Anonymous type error at my foreach loop.
我陷入了Linq / Lambda语句,在我的foreach循环中不断收到匿名类型错误。 I'm pretty new to C# so any help is appreciated.
我是C#的新手,所以可以提供任何帮助。
I'm using VS2013 我正在使用VS2013
Here is my code: 这是我的代码:
Using LinqToExcel to read in the spreadsheet: 使用LinqToExcel在电子表格中阅读:
... also, on a previous project that is very similar to this i used a Dictionary like below, but in the previous project i was doing a lot of calculations that i had to add to the dictionary then enumerate over. ……此外,在与之非常相似的先前项目中,我使用了如下所示的Dictionary,但在先前的项目中,我做了很多计算,因此我不得不将其添加到字典中,然后进行枚举。 So i'm wondering if I should keep this as a dictionary or use List....
所以我想知道我是否应该将其保留为字典或使用列表...。
public static class BillingPagerProcess
{
private static Dictionary<BillingPagerSource, decimal> UseLinqToExcel(string path)
{
Dictionary<BillingPagerSource, decimal> dictionary = new
Dictionary<BillingPagerSource, decimal>();
var excel = new ExcelQueryFactory();
excel.FileName = path;
var pagerObject = (from p in excel.Worksheet<ModlPagers>("pagers")
where p.PagerPhoneNumber != "" // || 0000000000
select p).ToList();
Here is where i'm doing the GroupBy 这是我在做GroupBy的地方
var pagerObjectGrouped = pagerObject.GroupBy(pg => pg.PagerPhoneNumber)
.Select(s => new {
holder = s.First().Holder,
recordId = s.First().RecordId,
pagerNumber = s.First().PagerPhoneNumber,
sum = s.Sum(a => a.Amount)}).ToList();
I would like to do something like this to shorten this up, but i'm unable to select multiple 我想做这样的事情来缩短时间,但我无法选择多个
var pagerBillingObject = from x in excel.Worksheet<ModlPagers>("pagers").ToList()
group x by x.PagerPhoneNumber into pagerGrouped
select pagerGrouped.First(),
pagerGrouped.First().Holder,
pagerGrouped.Sum(x => x.Amount);
And here is where i am doing the DB insert 这是我正在做数据库插入的地方
NWSPortal.Services.NWSEntities pagerallcommit = new Services.NWSEntities();
// *this is where I get the Anonymous Type error - on my foreach statement*
foreach (ModlPagers row in pagerObjectGrouped)
{
NWSPortal.Services.Pager pagerall = new Services.Pager();
pagerall.id = row.RecordId;
pagerall.PagerPhoneNumber = row.PagerPhoneNumber;
pagerall.Holder = row.Holder;
pagerallcommit.Pagers.Add(pagerall);
}
pagerallcommit.SaveChanges();
return dictionary;
}
In your for each loop you are indicating row is of type ModlPagers, but its actually an anonymous type based on how your select is for pagerObjectGrouped. 在for每个循环中,您指示行的类型为ModlPagers,但实际上它是基于您为pagerObjectGrouped选择的方式的匿名类型。 If you want to iterate over ModlPagers in your loop then you can make this change:
如果要在循环中遍历ModlPagers,则可以进行以下更改:
var pagerObjectGrouped = pagerObject.GroupBy(pg => pg.PagerPhoneNumber)
.Select(s => new ModlPagers {
Holder = s.First().Holder,
RecordId = s.First().RecordId,
PagerPhoneNumber = s.First().PagerPhoneNumber,
Sum = s.Sum(a => a.Amount)}).ToList();
Notice the new ModlPagers
part. 注意
new ModlPagers
部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.