5

我有一个用 VS2012 编写的 asp.net 应用程序。在移至 VS2015 之前,我一直在使用 LinqToExcel,没有任何问题。这是我的代码:

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

                var entries = entriesQuery.ToList();

V入门类

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; }
    }

它在 VS2012 上运行良好。当我在 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)

但是当我在 VS2012 中构建它时,它又可以工作了。可能有什么问题?我不知道。

编辑:在 VS2013 上它也可以工作。

4

3 回答 3

0

我知道旧帖子,但在使用 Visual Studio 2010 多年后,我无法让 LinqToExcel 与 Visual Studio 2017 一起使用。仅设置 ExcelQueryFactory 就会爆炸。无论如何,在从 Nuget 获得最新版本但仍然失败后,我决定编写自己的版本(见下文)。简短而甜蜜,您将一个空 List(Of YourObjectName) 和一个 Excel 互操作工作表传递给它,该工作表的第一行列标题逐字显示对象中的属性名称。该函数将标题行下方的所有行作为对象返回,从那里我可以对它们执行我自己的 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
于 2019-06-13T06:11:58.617 回答
0

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

于 2017-05-10T01:47:21.987 回答
0

我假设“MovieTitle”在您的模型中不接受空值。如果这是真的,请将您的 where 子句更改为以下。

var entriesQuery = from entry in excel.Worksheet<VEntry>(0)
                                  where entry.MovieTitle != string.Empty
                                  select entry;
于 2016-10-04T12:24:29.167 回答