c# - Int[].Contains 在 EF6 中不起作用

标签 c# linq entity-framework

我有一个奇怪的问题,基本上我有这段代码:

var langauges = (from l in context.languages 
                 where Model.LanguageIDs.Contains(l.LanguageID) 
                 select l)
                .ToList<language>();

我不知道为什么,但我总是得到一个错误:

System.NullReferenceException: Object reference not set to an instance of an object

问题是它之前工作正常,但在我将我的所有应用程序升级到 MVC5 和 EF6 之后,它开始因这段代码而崩溃。

虽然如果我检查 Model.Langauges,它有多个 ID,所以没问题。

我还遍历了 context.Languages,它们都有 id。

我也是这样写的:

var langauges = (from l in context.languages 
                 where 1==l.LanguageID 
                 select l)
                .ToList<language>();

这也行,所以我真的不明白,我做错了什么?

谁能详细说明...?

堆栈跟踪:

   at MySql.Data.Entity.SqlGenerator.Visit(DbPropertyExpression expression)
   at MySql.Data.Entity.SqlGenerator.Visit(DbInExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbInExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SqlGenerator.VisitBinaryExpression(DbExpression left, DbExpression right, String op)
   at MySql.Data.Entity.SqlGenerator.Visit(DbAndExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbAndExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SelectGenerator.Visit(DbFilterExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbFilterExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type)
   at MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type)
   at MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree)
   at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory)
   at System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver)
   at System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator)
   at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__a()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9()
   at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MyProject.Infrastructure.DAL.Operations.UpdateUserinfo(Int32 id, Model Model) in :line 168

最佳答案

是否有可能 Model.LanguageIDsnull 或您的 context 尚未设置?

此外,您是否允许 context.languages 中的 null language 条目?如果其中任何一个为 null,您将得到此异常。

关于c# - Int[].Contains 在 EF6 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21028973/

相关文章:

c# - 在 Linq EF 中使用 include 时修复循环

c# - Entity Framework ,导航属性和存储库模式

c# - Thread.CurrentThread.Join() 什么时候有意义?

c# - 异常处理中函数参数的一般记录

c# - .net IEnumerable 除了自定义 IEqualityComparer 没有按预期工作

asp.net-mvc - 添加迁移 : Value does not fall within expected range

c# - 创建用户时读取超时错误c#

c# - 获取目录名称的数字版本时出错

c# - 使用 Linq to Entities 读取 byte[] 或 float

c# - 尝试创建新表时对象名称无效