1

The following code is showing this error

Object must implement IConvertible

when I am adding the where clause in the LINQ expression to compare the DateTime field.

I tried to use Convert.ToDateTime(c.ETC) >= startday but still the same error.

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);

Definition of 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; }
}
amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • 1
    What is the type of `ETC`? – Matt Burland Jul 21 '15 at 20:19
  • 1
    Posting the definition of `BulkMovementItem` would help. – Jacob Jul 21 '15 at 20:21
  • And I assume this is the [LinqToExcel](https://github.com/paulyoder/LinqToExcel) library? It would be helpful if you include that in your question. – Matt Burland Jul 21 '15 at 20:21
  • I'm going to guess that `ETC` is a string? In that case, you are going to need to [parse it](https://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx). – Matt Burland Jul 21 '15 at 20:24
  • ETC is a string field. – user3802566 Jul 22 '15 at 15:16
  • I can fix this by adding Tolist() as below before where clause. but I don't want to do that way as I am reading the whole list before filtering. My objective here to improve the performance of this query var newrailtruckmovements = (from c in excel.Worksheet(sheetname).ToList() where Convert.ToDateTime(c.ETC) >= StartDate && Convert.ToDateTime(c.ETC) < EndDate select c); – user3802566 Jul 22 '15 at 15:17
  • @Matt Burland - I parsed it as below but no luck. excel.AddMapping(x => x.ETC, "ETC", s => String.IsNullOrEmpty(s) ? DateTime.MinValue : DateTime.ParseExact(s, @"MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)); – user3802566 Jul 22 '15 at 15:37

1 Answers1

1

You just need to change ETC's type in your class to DataTime. The library will do the rest:

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

And this would work:

var rows = from c in excel.Worksheet<BulkMovementItem>(sheetname)                  
           where c.ETC > new DateTime(2015, 7, 1) 
           select c;
amiry jd
  • 27,021
  • 30
  • 116
  • 215