简体   繁体   English

Visual Studio 2015 LinqToExcel不起作用

[英]Visual Studio 2015 LinqToExcel doesn't work

I have an asp.net app written in VS2012. 我有一个用VS2012编写的asp.net应用程序。 I was using LinqToExcel without any problems until I moved to VS2015. 在我搬到VS2015之前,我一直在使用LinqToExcel而没有任何问题。 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 VEntry类

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. 它在VS2012上运行良好。 When I build it in VS2015 I get exception at line 当我在VS2015中构建它时,我会在线上获得异常

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. 但是当我在VS2012中构建它时,它再次起作用。 What may be wrong? 可能有什么不对? I have no idea. 我不知道。

Edit: On VS2013 it works too. 编辑:在VS2013上也可以。

I assume 'MovieTitle' does not accept null values in your model. 我假设'MovieTitle'不接受模型中的空值。 If that's true, please change your where clause to following. 如果这是真的,请将您的where子句更改为以下内容。

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

vs 2015使用roselyn编译器和codedom提供程序,删除它们

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. 我知道的旧帖子,但是在使用Visual Studio 2010多年后,我无法让LinqToExcel与Visual Studio 2017一起工作。只需设置ExcelQueryFactory即可。 Anyway, after getting latest from Nuget and still having it fail, I decided to code up my own version (see below). 无论如何,在从Nuget获得最新信息并且仍然失败后,我决定编写自己的版本(见下文)。 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. 简短而甜蜜,您传递一个空的List(Of YourObjectName)和一个Excel Interop工作表,它具有第一行列标题,逐字逐句显示对象中属性的名称。 The function returns all the rows below the header row as objects, and from there I can do my own linq on them. 该函数返回标题行下面的所有行作为对象,从那里我可以在它们上面做我自己的linq。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM