Linq to Excel - 对象必须实现IConvertible错误

时间:2015-07-21 19:33:16

标签: c# linq linq-to-excel

以下代码显示此错误

  

对象必须实现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; }
}

1 个答案:

答案 0 :(得分:1)

您只需将班级中的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;