我有以下實體框架7個實體:獲取表第2列的關係
public class EbookFile {
public Int32 EbookId { get; set; }
public Int32 FileId { get; set; }
public virtual Ebook Ebook { get; set; }
public virtual File File { get; set; }
}
public class File {
public Int32 Id { get; set; }
public Byte[] Content { get; set; }
public String Name { get; set; }
public virtual ICollection<EbookFile> EbooksFiles { get; set; }
}
給定一個EbookId我需要得到的ID和與之相關的所有文件的名稱:
Dictionary<Int32, Dictionary<String, Int32>> files = await _context
.EbooksFiles
.Include(x => x.File)
.Where(x => result.Select(y => y.Id).Contains(x.EbookId))
.GroupBy(x => x.EbookId)
.ToDictionaryAsync(x => x.Key, x => x.ToDictionary(y => y.File.Name, y => y.File.Id));
的問題是,我也得到我不想要的內容:
SELECT [x].[EbookId], [x].[FileId], [f].[Id], [f].[Content], [f].[Name]
FROM [EbooksFiles] AS [x]
INNER JOIN [Files] AS [f] ON [x].[FileId] = [f].[Id]
我怎樣才能提高我的LINQ expre ssion只將FileId和Name放入字典中?
TEST QUERY
var ids = new Int32[] { 1, 2 };
var files = _context
.EbooksFiles
.Where(x => ids.Contains(x.EbookId))
.Select(x => new { x.EbookId, x.File.Name, x.File.Id })
.GroupBy(x => x.EbookId)
.ToList();
棧跟蹤
2016年4月29日十五時13分40秒[錯誤]已經在執行請求 System.InvalidOperationException發生未處理的異常:序列在System.Linq.Enumerable.Single [TSource](IEnumerable 1 source) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.<>c__DisplayClass79_0
1.b__0(IEnumerable 1 ps, IQuerySource qs) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpressionCore[TResult](MemberExpression memberExpression, IQuerySource querySource, Func
3 memberBinder)包含多個元素在Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression [TResult](MemberExpression memberExpression,IQuerySource querySource,函數功能3 memberBinder) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression(MemberExpression memberExpression, Action
2 memberBinder) 在Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.VisitMember(MemberExpression memberExpression) 在System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor訪問者) 在Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(表達式表達) 在System.Linq.Expressions.ExpressionVisitor.VisitAndConvert [T](ReadOnlyCollection 1 nodes, String callerName) at Remotion.Linq.Parsing.RelinqExpressionVisitor.VisitNew(NewExpression expression) at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor) at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression) at Remotion.Linq.Clauses.ResultOperators.GroupResultOperator.TransformExpressions(Func
2轉換) at Remotion.Linq.QueryModel.TransformExpressions(Func 2 transformation) at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.FindQuerySourcesRequiringMaterialization(QueryModel queryModel) at Microsoft.Data.Entity.Query.QueryCompilationContext.FindQuerySourcesRequiringMaterialization(EntityQueryModelVisitor queryModelVisitor, QueryModel queryModel) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) at Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel queryModel) --- End of stack trace from previous location where exception was thrown --- at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_0
1.b__0() at Microsoft.Data.Entity.Query.Internal。 CompiledQueryCache.GetOrAddQuery [TResult](對象cacheKey,函數功能1 compiler) at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query) at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at Remotion.Linq.QueryableBase
1.GetEnumerator() 在System.Collections.Generic.List 1..ctor(IEnumerable
1集合) 在System.Linq.Enumerable.ToList [TSource](IEnumerable的1 source) at EbookGetApiModelHandler.QueryHandler.<Handle>d__4.MoveNext() in \Handlers\EbookGetApiModelHandler.cs:line 85 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MediatR.Mediator.<SendAsync>d__4
1.MoveNext() ---從先前的位置在那裏引發異常--- 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務task) 堆棧跟蹤的結尾處System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務task) 在System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at Controllers.EbookApiController.<Get>d__2.MoveNext() in \Controllers\EbookApiController.cs:line 30 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Mvc.Controllers.ControllerActionExecutor.<CastToObject>d__8
1.MoveNext() ---拋出異常的上一個位置的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskA waiter.ThrowForNonSuccess(任務任務) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務) at Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker.d__6.MoveNext() ---從前一個位置結束堆棧跟蹤其中引發異常--- 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務task) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務task) 在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任務任務) at Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__53。MoveNext() ---上一個位置拋出異常的堆棧跟蹤結束--- at Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__44.MoveNext() ---上一個位置的堆棧跟蹤結束位置異常被拋出--- 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務task) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務task) 在Microsoft.AspNet.Mvc.Infrastructure.MvcRouteHandler.d__6。 MoveNext() ---從之前位置拋出異常的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger通知(任務任務) at Microsoft.AspNet.Mvc.Routing.InnerAttributeRoute.d__10.MoveNext() ---從之前位置拋出異常的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter。 ThrowForNonSuccess(任務任務) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務) at Microsoft.AspNet.Routing.RouteCollection.d__9.MoveNext() ---從上一個位置拋出異常的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Builder.RouterMiddleware.d__4.MoveN EXT() ---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務task) 從先前的位置堆棧跟蹤,其中引發異常--- 結束在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務) at Microsoft.AspNet.Authentication.AuthenticationMiddleware 1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNet.Authentication.AuthenticationMiddleware
1.d__18.MoveNext() ---拋出異常的上一個位置的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task ) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Authentication.AuthenticationMiddleware 1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNet.Authentication.AuthenticationMiddleware
1.d__18.MoveNext() ---從以前位置拋出異常的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Authentication.AuthenticationMiddleware 1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNet.Authentication.AuthenticationMiddleware
1.d__18.MoveNext() ---從以前位置拋出異常的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務) at Microsoft.AspNet.Authentication.AuthenticationMiddleware 1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNet.Authentication.AuthenticationMiddleware
1.d__18.MoveNext() ---堆棧跟蹤結束米以前的位置在那裏引發異常--- 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務task) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務task) 在Microsoft.AspNet.Localization.RequestLocalizationMiddleware。 d__4.MoveNext() ---從以前位置拋出異常的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務) at Microsoft.AspNet.Diagnostics.Entity.MigrationsEndPointMiddleware.d__5。MoveNext() ---從之前位置拋出異常的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task ) at Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.d__6.MoveNext() ---上一個位置拋出異常的堆棧跟蹤結束--- 位於Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.d__6。 MoveNext() ---從之前的位置拋出異常的堆棧跟蹤結束--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務) at System.Runtime.CompilerServices.TaskAwaiter.Handl eNonSuccessAndDebuggerNotification(任務task) 在Microsoft.AspNet.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()