Linq to Excel -对象必须实现不可逆转错误

本文关键字:实现 不可逆转 错误 对象 to Excel Linq | 更新日期: 2023-09-27 18:03:47

下面的代码显示了这个错误

对象必须实现IConvertible

当我在LINQ表达式中添加where子句来比较DateTime字段时。

我尝试使用Convert.ToDateTime(c.ETC) >= startday,但仍然是相同的错误。

var excel = new ExcelQueryFactory(excelfilename);
excel.AddMapping<BulkMovementItem>(x => x.ETC, "ETC");
var newrailtruckmovements = (from c in excel.Worksheet<BulkMovementItem>(sheetname)                  
    where c.ETC > new DateTime(2015, 7, 1) 
    select c);

BulkMovementItem定义:

public class BulkMovementItem
{
    public string  ScheduleName { get; set; }   
    public string  DealHeaderName { get; set; } 
    public string DealDetailName { get; set; }
    public string ETC { get; set; }
    public string RailcarName { get; set; } 
    public string Location   { get; set; }
    public string OriginLocation { get; set; }  
    public string FunctionType { get; set; } 
    public string ProductName { get; set; } 
    public string Volume { get; set; }  
    public string SupplierUniqueNbr { get; set; }
    // Error Description
    public string Status { get; set; }
    public bool HasErrors { get; set; }
    //public List<string> ErrorDetails { get; set; }
}

Linq to Excel -对象必须实现不可逆转错误

您只需将类中的ETC的类型更改为DataTime即可。库将完成剩下的工作:

public class BulkMovementItem {
    // some fields...
    public DateTime ETC { get; set; }
    // rest of fields...
}

可以这样写:

var rows = from c in excel.Worksheet<BulkMovementItem>(sheetname)                  
           where c.ETC > new DateTime(2015, 7, 1) 
           select c;