5

I have an asp.net app written in VS2012. I was using LinqToExcel without any problems until I moved to VS2015. Here is my code:

var excel = new ExcelQueryFactory(fileName);
var entriesQuery = from entry in excel.Worksheet<VEntry>(0)
                                      where entry.MovieTitle != null
                                      select entry;

                var entries = entriesQuery.ToList();

VEntry class

public class VEntry
    {
        [ExcelColumn("id kolekcji")]
        [DefaultValue("")]
        public String CollectionId { get; set; }

        [ExcelColumn("nazwa kolekcji")]
        [DefaultValue("")]
        public String CollectionName { get; set; }

        [ExcelColumn("Tytuł serialu/serii/filmu")]
        [DefaultValue("")]
        public String MovieTitle { get; set; }

        [ExcelColumn("Tytuł odcinka")]
        [DefaultValue("")]
        public String EpisodeTitle { get; set; }

        [ExcelColumn("Sezon")]
        [DefaultValue("")]
        public String Season { get; set; }

        [ExcelColumn("nr odcinka")]
        [DefaultValue("")]
        public String EpisodeNumber { get; set; }

        [ExcelColumn("Start")]
        public DateTime StartDate { get; set; }

        [ExcelColumn("Koniec")]
        public DateTime EndDate { get; set; }

        [ExcelColumn("Kategoria tematyczna")]
        [DefaultValue("")]
        public String Category { get; set; }

        [ExcelColumn("Cena")]
        [DefaultValue("")]
        public String Price { get; set; }

        [ExcelColumn("kategoria wiekowa")]
        [DefaultValue("")]
        public String AgeCategory { get; set; }

        [ExcelColumn("Seria (0/1)")]
        [DefaultValue("")]
        public bool IsSeries { get; set; }

        [ExcelColumn("box set (0/1)")]
        [DefaultValue("")]
        public bool IsBoxSet { get; set; }

        [ExcelColumn("Cały sezon")]
        [DefaultValue("")]
        public bool IsFullSeason { get; set; }
    }

It worked fine on VS2012. When I build it in VS2015 I get exception at line

var entries = entriesQuery.ToList();

:

Object must implement IConvertible.

   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at LinqToExcel.Extensions.CommonExtensions.Cast(Object object, Type castType)
   at LinqToExcel.Extensions.CommonExtensions.Cast[T](Object object)
   at LinqToExcel.Extensions.CommonExtensions.IsNullValue(Expression exp)
   at LinqToExcel.Query.WhereClauseExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression bExp)
   at LinqToExcel.Query.SqlGeneratorQueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
   at Remotion.Data.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
   at LinqToExcel.Query.SqlGeneratorQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
   at LinqToExcel.Query.ExcelQueryExecutor.GetSqlStatement(QueryModel queryModel)
   at LinqToExcel.Query.ExcelQueryExecutor.ExecuteCollection[T](QueryModel queryModel)
   at Remotion.Data.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteQueryModel(QueryModel queryModel, IQueryExecutor executor)
   at Remotion.Data.Linq.QueryProviderBase.Execute[TResult](Expression expression)
   at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Logic.Importers.VodImporter.VodImporter.Run(String fileName, Boolean publishAfterImport) in C:\ncplus\npl\Logic\Importers\VodImporter\VodImporter.cs:line 103
   at Website.sitecore_modules.Shell.Editors.VodImporterEditor.Page_Load(Object sender, EventArgs e)

But when I build it in VS2012 it works again. What may be wrong? I have no idea.

Edit: On VS2013 it works too.

leppie
  • 115,091
  • 17
  • 196
  • 297
petros
  • 705
  • 1
  • 8
  • 26
  • did you also upgrade the .NET Framework or remain the same? most likely you also upgraded the .NET Framework for the solution – Ahmed ilyas Nov 09 '15 at 14:06
  • What .NET version are you using in VS2015? – Jamie Rees Nov 09 '15 at 14:06
  • It was .NET 4.5 and still is – petros Nov 09 '15 at 14:34
  • I'm also having the exact same error!! Have you sort this out as yet? I'm also on VS 2015 and .NET 4.5. It worked fine on VS 2013 and .NET 4.5. It doesn't seem to read the excel file. I'm kinda lost!! By the way I'm on Win10. – ecasper Jan 27 '16 at 22:55

3 Answers3

0

I assume 'MovieTitle' does not accept null values in your model. If that's true, please change your where clause to following.

var entriesQuery = from entry in excel.Worksheet<VEntry>(0)
                                  where entry.MovieTitle != string.Empty
                                  select entry;
ecasper
  • 489
  • 1
  • 10
  • 30
0

vs 2015 use roselyn compiler and codedom provider, remove them

0

Old post I know but I'm unable to get LinqToExcel to work with Visual Studio 2017 after using it for years with Visual Studio 2010. Blows up just setting up the ExcelQueryFactory. Anyway, after getting latest from Nuget and still having it fail, I decided to code up my own version (see below). Short and sweet, you pass it an empty List(Of YourObjectName) and an Excel Interop worksheet, which has the 1st row column headers verbatim the name of the properties in the object. The function returns all the rows below the header row as objects, and from there I can do my own linq on them. Hope this helps someone.

 Private Sub GetObjectsFromExcelWorksheet(ByRef listObjects As Object, '
                                             ByVal ws As Microsoft.Office.Interop.Excel.Worksheet)
        'Fancy code that gets the type of the objects passed in a list
        Dim objType = (listObjects.GetType.GetGenericArguments())(0)

        Dim headers As New List(Of String)
        Dim A1 = ws.Range("A1")
        Dim nHeaders = ws.UsedRange.Columns.Count
        For i = 0 To nHeaders - 1
            Dim header = A1.Offset(0, i).Value
            headers.Add(header)
        Next
        For i = 1 To ws.UsedRange.Rows.Count - 1
            Dim newObj = Activator.CreateInstance(objType)
            For j = 0 To nHeaders - 1
                CallByName(newObj, headers(j), CallType.Set, A1.Offset(i, j).Value)
            Next
            listObjects.Add(newObj)
        Next
    End Sub