Search code examples
c#linq-to-excel

Why LinqToExcel is returning null when encounters "-" character?


I'm using LinqToExcel to retrieve some information and I can get the data from all my columns except from one. This is the Scenario:

I have 3 columns:

Code   Name   LastName
 89    test1   test2
 89-2  test3   test4
 90    test5   test6

and to get the list of values I use:

var excel = new LinqToExcel.ExcelQueryFactory(path)
var excelValues = excel.Worksheet<Sheet>("Sheet1").AsEnumerable().Where(p => !String.IsNullOrWhiteSpace(p.Code));

This returns the following information:

89  |test1|test2
null|test3|test4 //here is my problem
90  |test5|test6

If I change the 89-2 to be 89.2 then it works. Is there any way I can escape the "-" character? or what I'm doing wrong.


Solution

  • LinqToExcel uses the 2nd row to determine datatype, 89 is being considered a number and all subsequent data is considered to be a number. Add a single quote (') prior to the number, e.g. '89 to force excel to set the contents as text.

    The first row is used to map column names to class properties