簡體   English   中英

EntityFramework NotSupportedException與嵌入式Join聯接

[英]EntityFramework NotSupportedException Join with embedded Join

由於某些原因,下面的代碼會出現此錯誤:

EntityFramework.SqlServer.dll中發生類型為'System.NotSupportedException'的異常,但未在用戶代碼中處理

附加信息:LINQ to Entities無法識別方法'System.Linq.IQueryable 1[DansBlog.Domain.BlogEntities.Post] Retrieve(System.Linq.Expressions.Expression 1 [System.Func`2 [DansBlog.Domain.BlogEntities。 Post,System.Boolean]])'方法,並且該方法無法轉換為商店表達式。

碼:

var blogRepository = this.repositoryFactory.Create<Blog>(unitOfWork);
var userRepository = this.repositoryFactory.Create<User>(unitOfWork);
var postRepository = this.repositoryFactory.Create<Post>(unitOfWork);

var allResult = blogRepository.Retrieve().Join(
    userRepository.Retrieve(),
    b => b.UserId,
    u => u.Id,
    (blog, user) => new GUI.Models.Blogging.Blog()
    {
        Id = blog.Id,
        Content = blog.Content,
        Title = blog.Title,
        DateAdded = blog.DateAdded,
        User = new GUI.Models.Blogging.User()
        {
            UserId = user.Id,
            FirstName = user.FirstName,
            LastName = user.LastName,
            Email = user.Email,
            UserName = user.UserName
        },
        Posts =  postRepository.Retrieve(p => p.BlogId.Equals(blog.Id)).Join(
            userRepository.Retrieve(),
            p => p.UserId,
            u => u.Id,
            (post, postUser) => new GUI.Models.Blogging.Post()
                {
                    Content = post.Content,
                    DateAdded = post.DateAdded,
                    Id = post.Id,
                    User = new GUI.Models.Blogging.User()
                    {
                        UserId = postUser.Id,
                        UserName = postUser.UserName,
                        FirstName = postUser.FirstName,
                        LastName = postUser.LastName,
                        Email = postUser.Email
                }})});

LINQ to Entities不支持您對postRepository.Retrieve的子調用。 為了使這項工作有效,您需要重新構造查詢,並使用GroupJoin方法在查詢中更早地提取Posts集合,並使用匿名類型將Posts與Blog結合起來。 結果查詢結構應如下所示:

var allResult = blogRepository.Retrieve()
               .Join(userRepository.Retrieve(), b => b.UserId, u => u.Id, (blog, user) => new { blog, user})
               .GroupJoin(postRepository.Retrieve(), 
                                     .GroupJoin(userRepository.Retrieve(), p => p.UserId, pu => pu.Id, 
                                        (post, authors) => new GUI.Models.Blogging.Post()
                                        {
                                            Id = post.Id,
                                            BlogId = post.BlogId,
                                            User = authors.Select(pa => new GUI.Models.Blogging.User()
                                            {
                                                UserId = pa.Id,
                                                UserName = pa.UserName,
                                                // etc
                                            }).FirstOrDefault()
                                        }),
                      blogData => blogData.blog.Id, p => p.BlogId, (blogData, posts) => new { blogData, posts })
                 .Select(projection => new GUI.Models.Blogging.Blog()
                 {
                    Id = projection.blogData.blog.Id,
                    // etc
                    User = new GUI.Models.Blogging.User()
                    {
                        UserId = projection.blogData.user.Id,
                        UserName = projection.blogData.user.UserName,
                        // etc
                    },
                    Posts =  projection.posts
                 }
             );

請注意,使用Method語法編寫的LINQ查詢的可讀性不高,乍一看似乎並不明顯。 考慮在可能的情況下使用Query語法 (如果無法使用,請回退到Method),因為它可以提高可讀性並在大多數情況下使您的代碼更簡潔。 例如,上面的查詢看起來像:

var allResult = from blog in blogRepository.Retrieve()
                join user in userRepository.Retrieve() on b.UserId equals u.Id
                join p in postRepository.Retrieve()
                                        .GroupJoin(userRepository.Retrieve(), p => p.UserId, pu => pu.Id, 
                                            (post, authors) => new GUI.Models.Blogging.Post()
                                            {
                                                Id = post.Id,
                                                BlogId = post.BlogId,
                                                User = (from pu in authors
                                                        select new GUI.Models.Blogging.User()
                                                        {
                                                            UserId = pu.Id,
                                                            UserName = pu.UserName,
                                                            // etc
                                                        }).FirstOrDefault()
                                            }),
                          on blog.Id equals p.BlogId into posts
                 select new GUI.Models.Blogging.Blog()
                     {
                        Id = blog.Id,
                        // etc
                        User = new GUI.Models.Blogging.User()
                        {
                            UserId = user.Id,
                            UserName = user.UserName,
                            // etc
                        },
                        Posts = posts
                     }
                 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM