関係

2016-04-29 10 views
1

の表の2列を取得します。私は、次のEntity Frameworkの7つの実体があります関係

私はそれに関連するすべてのファイルのIDと名前を取得する必要がありますEbookId考える
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; } 
} 

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を向上させることができますFileIdとNameだけを辞書に入れるには?要求 System.InvalidOperationExceptionが実行中

TESTクエリ

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日15時13分40秒[ERROR]未処理の例外が発生しました。シーケンスに複数の要素が含まれています 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.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.VisitMember(MemberExpression memberExpression)でMicrosoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression [TResult(MemberExpression memberExpression、IQuerySource querySourceの、のFunc 3 memberBinder) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression(MemberExpression memberExpression, Action 2 memberBinder)における System.Linq.Expressions.ExpressionVisitor.VisitAndConvertでMicrosoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(式発現) [T]でSystem.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor訪問者) (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変換) 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() 、Microsoft.Data.Entity.Query.Internal。 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.Collections.Generic.List 1..ctor(IEnumerable 1つのコレクションにおけるオブジェクトcacheKey、のFunc 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() )CompiledQueryCache.GetOrAddQuery [TResult] 例外がスローされた以前の場所からのスタックトレースの ---終わり--- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccessで (タスクタスク) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificationで(タスクのタスク) で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(タスクのタスク) でSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificationで(タスクタスク) System.Runtime.CompilerServices.TaskAwaiter.ValidateEndで(タスクタスク) (Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__53)MoveNext() ---例外がスローされた前の場所からのスタックトレースの終了--- at Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__44.MoveNext() ---前の場所からのスタックトレースの終わり例外がスローされた--- をSystem.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスクのタスク) でSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificationで(タスクタスク) Microsoft.AspNet.Mvc.Infrastructure.MvcRouteHandler.d__6で。 MoveNext() ---例外がスローされた前の場所からのスタックトレースの末尾---at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスクタスク) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebugger通知(タスクタスク) 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(タスクタスク) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(タスクタスク) 、Microsoft.AspNet.Builder.RouterMiddleware.d__4.MoveN EXT() --- System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificationでSystem.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスクのタスク) で例外がスローされた以前の位置からスタックトレース--- の終了(タスクのタスク) 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(タスクタスク) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(タスクタスク) 、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(タスクタスク)で 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() ---例外がスローされた前の場所からのスタックトレースの末尾---at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスクタスク) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(タスクタスク) 、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() ---スタックトレースの終わりMicrosoft.AspNet.Localization.RequestLocalizationMiddlewareに(タスクのタスク) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificationでSystem.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスクタスク) に例外がスローされた以前の位置--- をMです。 dup4.MoveNext() ---例外がスローされた前の場所からのスタックトレースの末尾---at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスクタスク) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificationタスクタスク) (Microsoft.AspNet.Diagnostics.Entity.MigrationsEndPointMiddleware.d__5)MoveNext() ---例外がスローされた前の場所からのスタックトレースの末尾---at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスクタスク) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(タスクタスク) 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(タスクタスク)Microsoft.AspNet.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()

答えて

0

で 私はあなたのテストクエリで.Include(x => x.File)を忘れてしまったと思います