我正在尝试使用LINQ to Excel并将列映射到对象属性,但我有一个非常奇怪的运行时错误,我无法理解导致它的原因。错误说明:
LinqToExcel.dll中出现未处理的“System.Data.DataException”类型异常
'oDateTime(-1.EDI_date'不是有效的列名。有效列名为:'EDI Date','Week No','Dist No#','Cust Type' ,'Cust No#','Site No#','Date','Time','Card No#','Reg','Bunker Sheet','Mileage',
这很奇怪,因为我从未通过名称“oDateTime(-1.EDI_date”)引用任何列,似乎将Convert.ToDateTime函数解析为字符串或其他内容?
以下块是执行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; } }
}