EntityFramework 6.4.4 - 如何将集合投影到实现 IEnumerable的自定义对象?

发布于 2025-01-20 04:06:27 字数 3128 浏览 3 评论 0原文

无法映射到实现 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(Nullable1 forMergeOption) 在 System.Data.Entity.Core.Objects.ObjectQuery1。<>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.ObjectQuery1.GetResults(Nullable1 forMergeOption)在System.Data.Entity.Core.Objects.ObjectQuery1.b__31_0() 在 System.Data.Entity.Internal.LazyEnumerator1.MoveNext() 在 System.Collections.Generic.List1..ctor(IEnumerable1 集合) 在 System.Linq.Enumerable.ToList [TSource](IEnumerable1 源)

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.ObjectQuery1.<>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.ObjectQuery1.<>c__DisplayClass41_0.b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable.GetEnumerator>b__31_0()
at System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2025-01-27 04:06:27

EF 无法理解/启动该类。它旨在适应对简单类和集合的投影。如果您觉得必须做类似的事情,那么可以进行双重项目:

var userDetails dbContext.Users.Select(x => new 
{
    BranchIds = x.Branches.Select(b => b.BranchId).ToList()
}).ToList()  // This executes the query getting our branch IDs (and include anything from the User we need as well.
.Select(x => new UserDto
{
    Collection = new BranchCollection { BranchIds = x.BranchIds }
}.ToList(); // Project to your custom type from memory.

对于这样的类,您想要包装 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:

var userDetails dbContext.Users.Select(x => new 
{
    BranchIds = x.Branches.Select(b => b.BranchId).ToList()
}).ToList()  // This executes the query getting our branch IDs (and include anything from the User we need as well.
.Select(x => new UserDto
{
    Collection = new BranchCollection { BranchIds = x.BranchIds }
}.ToList(); // Project to your custom type from memory.

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 write

userDto.Collection = new BranchCollection();

... and probably gunk things up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文