Visual Studio 2015 LinqToExcel not working

I have an asp.net application written in VS2012. I used LinqToExcel without problems until I switched 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 great on VS2012. When I create it in VS2015, I get an exception in the 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 create it in VS2012, it works again. What could be wrong? I have no idea.

Edit: In VS2013, it also works.

+4
source share
3 answers

I assume that "MovieTitle" does not accept null values โ€‹โ€‹in your model. If true, modify the where clause as follows.

var entriesQuery = from entry in excel.Worksheet<VEntry>(0)
                                  where entry.MovieTitle != string.Empty
                                  select entry;
0
source

vs 2015 use roselyn compiler and codedom provider, delete them

0

, , LinqToExcel Visual Studio 2017 Visual Studio 2010. ExcelQueryFactory. , Nuget , (. ). , List (Of YourObjectName) Excel Interop, 1- . , linq . , - .

 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
0

Source: https://habr.com/ru/post/1615048/


All Articles