無法映射到實作 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; }
}
嘗試使用以下代碼投影到 UserDto
dbContext.Users.Select(
x => new UserDto {
Collection = new BranchCollection { BranchIds = x.Branches.Select(y => y.BranchId) }
}
.ToList();
例外
無法在 LINQ to Entities 查詢中初始化實作 IEnumerable 'BranchCollection' 的型別。
堆疊跟蹤
在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.CheckInitializerType(Type type) 在 System.Data. Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq) 在 System.Data.Entity.Core.Objects .ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator。 OneLambdaTranslator.Translate(ExpressionConverter 父級,System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) 在 System.Data.Entity.Core.Objects 的 MethodCallExpression 呼叫、DbExpression& 源、DbExpressionBinding& sourceBinding、DbExpression& lambda)。 ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert () 在 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(NullableExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) 在 System.Data.Entity.Core.Objects。 ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert() 在 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(NullableExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) 在 System.Data.Entity.Core.Objects。 ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert() 在 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(NullableTranslateExpression(Expression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert() 在 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(NullableTranslateExpression(Expression linq) 在 System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert() 在 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable
1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass41_0.b__1() 在 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() 在 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 源)
uj5u.com熱心網友回復:
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();
...而且可能會把事情搞砸。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/461438.html
