Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinqToExcel - InvalidCastException leading to Object must implement Iconvertible error

I'm using the following code to query an excel file in LinqToExcel:

var excelFile = new LinqToExcel.ExcelQueryFactory(@"\"+txtFileName.Text.Replace(@"\\",@"\"));

var properties = from p in excelFile.Worksheet<Property>()
                 where AssessmentID != null
                 select p;
foreach (var autoP in properties)
        doSomething();

When I look at the runtime debugger, I see an "InvalidCastException" while looking at the "Results View" of the properties variable. So, I'm assuming that there's something funky going on with my class definition. I'm also assuming that I don't need to map all members of the class to the excel file, but rather only the ones I see fit.
So, Here's the class definition as well:

public class Property
{

    [DataMember]
    public int? Year { get; set; }

    [DataMember]
    public string ChangeReason { get; set; }

    [DataMember]
    public string AssessmentID { get; set; }

    [DataMember]
    public string CallBackNotes { get; set; }

    [DataMember]
    public string InspectionNotes { get; set; } 

    [DataMember]
    public string Notes { get; set; }

    [DataMember]
    public bool Authorization { get; set; }

    [DataMember]
    public string ChargeStatus { get; set; }

    [DataMember]
    public string LegalLandDesc { get; set; }

    [DataMember]
    public string Address { get; set; }
    }

Here's a link to the source code for linqToExcel on GitHub:LinqToExcel The generics are more complicated than I've seen, and are something that I (apparently) need to brush up on.

Is there something glaring that I'm missing that would cause this issue? Any ideas as to where to look or what to do to resolve these errors?

like image 444
Rolan Avatar asked Dec 24 '22 10:12

Rolan


1 Answers

The where clause of the query is what was causing the error. It was referencing the null keyword which was not applicable in the context. I changed it to:

where  !String.IsNullOrEmpty(p.AssessmentID) 

This solved my issue.

Note to self: When inheriting code from others, check the basics first!

like image 181
Rolan Avatar answered Dec 27 '22 03:12

Rolan