为列名获取“不是有效的列名”我不提及,为什么?

我正在尝试开始使用LINQ to Excel,并将列映射到对象属性,但是我有一个非常奇怪的运行时错误,并且无法解决导致它的问题。 错误状态:

LinqToExcel.dll中发生未处理的types“System.Data.DataException”exception

'oDateTime(-1.EDI_date')不是有效的列名 ,有效的列名是:'EDI Date','Week No','Dist No#','Cust Type','Cust No#','Site No #“,”date“,”时间“,”卡号#“,”注册“,”仓单“,”里程“,

这是奇怪的,因为我永远不会引用任何列的名称“oDateTime(-1.EDI_date”似乎Convert.ToDateTime函数被parsing为一个string或东西?

下面的块是执行LINQ查询的函数:

public IEnumerable<EDI> getEDIs() { // Set date of last monday DateTime lastMonday = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek - 6); // Initialise Excel file var excelFile = new ExcelQueryFactory(DIRECTORY); // Create column mappings excelFile.AddMapping<EDI>(x => x.EDI_date, "EDI Date"); excelFile.AddMapping<EDI>(x => x.Week_no, "Week No"); excelFile.AddMapping<EDI>(x => x.Dist_no, "Dist No#"); excelFile.AddMapping<EDI>(x => x.Cust_no, "Cust No#"); excelFile.AddMapping<EDI>(x => x.Site_no, "Site No#"); excelFile.AddMapping<EDI>(x => x.Card_no, "Card No#"); excelFile.AddMapping<EDI>(x => x.Mileage, "Mileage"); excelFile.AddMapping<EDI>(x => x.Quantity, "Quantity"); excelFile.AddMapping<EDI>(x => x.Product_no, "Product No"); excelFile.AddMapping<EDI>(x => x.Month_no, "Month No#"); excelFile.AddMapping<EDI>(x => x.Month_week_no, "Week No#"); excelFile.AddMapping<EDI>(x => x.Transaction_no, "Transaction No#"); excelFile.AddMapping<EDI>(x => x.Band, "band"); // Declare LINQ query IEnumerable<EDI> EDIs = from edi in excelFile.Worksheet<EDI>("EDI Data") where DateTime.Compare(Convert.ToDateTime(edi.EDI_date) , lastMonday) <= 7 select edi; // Loop through results foreach (EDI edi in EDIs) { Console.WriteLine(edi.EDI_date); } return EDIs; } 

现在,错误指向foreach循环,相信它或不在“foreach”声明…这是导致DataException的特定行:

 > foreach (EDI edi in EDIs) 

此外,我的课程的代码:EDI如下,有2个构造函数

  class EDI { public EDI(string edi_date, int week_no, int cust_no, int dist_no, int site_no, int card_no, int mileage, int quantity, int product_no, int month_no, int month_week_no, int transaction_no, int value, int band) { this.edi_date = edi_date; this.week_no = week_no; this.cust_no = cust_no; this.dist_no = dist_no; this.site_no = site_no; this.card_no = card_no; this.mileage = mileage; this.quantity = quantity; this.product_no = product_no; this.month_no = month_no; this.month_week_no = month_week_no; this.transaction_no = transaction_no; this.value = value; this.band = band; } public EDI() { } readonly string edi_date; public string EDI_date { get { return edi_date; } } readonly int week_no; public int Week_no { get { return week_no; } } readonly int dist_no; public int Dist_no { get { return dist_no; } } readonly int cust_no; public int Cust_no { get { return cust_no; } } readonly int site_no; public int Site_no { get { return site_no; } } readonly int card_no; public int Card_no {get {return card_no;} } readonly int mileage; public int Mileage { get { return mileage; } } readonly int quantity; public int Quantity { get { return quantity; } } readonly int product_no; public int Product_no { get { return product_no; } } readonly int month_no; public int Month_no { get { return month_no; } } readonly int month_week_no; public int Month_week_no { get { return month_week_no; } } readonly int transaction_no; public int Transaction_no { get { return transaction_no; } } readonly int value; public int Value { get { return value; } } readonly int band; public int Band { get { return band; } } }