EntityFramework 6.4.4 - 如何将集合投影到实现 IEnumerable的自定义对象?
无法映射到实现 IEnumerable 的自定义对象?
public class BranchCollection : IEnumerable<int>
{
public IEnumerable<int> BranchIds { get; set; } = new List<int>();
public BranchPermission() { }
//Custom Code here
}
public class UserDto {
public BranchCollection Collection { get; set; }
}
尝试使用以下
dbContext.Users.Select(
x => new UserDto {
Collection = new BranchCollection { BranchIds = x.Branches.Select(y => y.BranchId) }
}
.ToList();
异常
代码投影到 UserDto无法在 LINQ to Entities 查询中初始化实现 IEnumerable“BranchCollection”的类型。
堆栈跟踪
在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.CheckInitializerType(类型类型) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter 父级,MemberInitExpression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式 linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter 父级,MemberInitExpression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式 linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda,DbExpression 输入) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter 父级、MethodCallExpression 调用、DbExpression& 源、DbExpressionBinding& sourceBinding、DbExpression& lambda) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter父级,MethodCallExpression调用) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter父级,MethodCallExpression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式 linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert() 在 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable
1 forMergeOption) 在 System.Data.Entity.Core.Objects.ObjectQuery
1。<>c__DisplayClass41_0.b__1( ) 在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategyexecutionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) 在 System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClass41_0.b__0() 在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 操作)在 System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption)在System.Data.Entity.Core.Objects.ObjectQuery
1.b__31_0() 在 System.Data.Entity.Internal.LazyEnumerator 1.MoveNext() 在 System.Collections.Generic.List
1..ctor(IEnumerable1 集合) 在 System.Linq.Enumerable.ToList [TSource](IEnumerable
1 源)
Cannot map to a custom object that implements IEnumerable ?
public class BranchCollection : IEnumerable<int>
{
public IEnumerable<int> BranchIds { get; set; } = new List<int>();
public BranchPermission() { }
//Custom Code here
}
public class UserDto {
public BranchCollection Collection { get; set; }
}
Trying to project to UserDto with code below
dbContext.Users.Select(
x => new UserDto {
Collection = new BranchCollection { BranchIds = x.Branches.Select(y => y.BranchId) }
}
.ToList();
Exception
A type that implements IEnumerable 'BranchCollection' cannot be initialized in a LINQ to Entities query.
Stack Trace
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.CheckInitializerType(Type type)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClass41_0.b__1()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClass41_0.b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1.<System.Collections.Generic.IEnumerable.GetEnumerator>b__31_0()
at System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EF 无法理解/启动该类。它旨在适应对简单类和集合的投影。如果您觉得必须做类似的事情,那么可以进行双重项目:
对于这样的类,您想要包装 IEnumerable 我建议将源集合传递到构造函数中以初始化私有成员并使其成为不可变,暴露您的枚举器等。如果可以添加/删除项目,那么可以使用类中的方法来容纳它,而不是暴露一个包含的集合,以后任何人都可以编写
userDto.Collection = new BranchCollection();
...并且可能会把事情搞砸。
EF cannot understand/initiate that class. It's designed to accommodate projection to simple classes and collections. If you feel you must do something like that then double-project:
For a class like that where you want to wrap
IEnumerable
I would recommend passing the source collection into a constructor to initialize a private member and make it immutable, exposing your enumerator etc. If items can be added/removed then accommodate that with methods in your class rather than exposing a contained collection where anyone later on could writeuserDto.Collection = new BranchCollection();
... and probably gunk things up.