簡體   English   中英

如何在asp.net mvc EF代碼中使用linq查詢關系1:N和N:N(在3個實體之間)從數據庫中獲取數據?

[英]how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?

我有一個linq查詢,用於從3個表(Post,Category,Tag)中獲取數據。

我想在博客的默認頁面中顯示最新帖子(10位數):帖子標題,帖子標簽名稱和帖子類別名稱,簡短說明。 我鍵入此linq查詢

List<Post> IBlogRepository.PostsForList(int pageNo, int pageSize)
    {
        using (var context = new MJBweblogContext())
        {
            IQueryable posts = from Post in context.Posts
                        where Post.Published == true
                        select new
                        {
                            Post.Title,
                            Post.ShortDescription,
                            Post.Description,
                            Post.MetaData,
                            Post.PostedOn,
                            Post.UrlSlug,
                            Post.Published,
                            Post.Modified,
                            Post.Category,
                            Post.Tags,
                            Post.Category.Name,
                        };
            if (posts != null)
            {
                return posts.OfType<Post>() // i give error if i remove OfType<Post>()
                    .OrderByDescending(p => p.PostedOn)
                    .Skip(pageNo * pageSize)
                    .Take(pageSize)
                    .ToList();
            }
            else
            {
                return null;
            }
        }
    }

但我有兩個問題:

  1. 如果我刪除OfType()VS2017告訴我:

    錯誤CS0266無法將類型“ System.Linq.IQueryable”隱式轉換為“ System.Collections.Generic.List”。 存在顯式轉換(是否缺少強制轉換?)MJBweblog C:\\ Users \\ mjb \\ source \\ repos \\ MJBweblog \\ MJBweblog \\ DAL \\ BlogRepository.cs 44活動

    1. 我不知道我應該寫什么代碼來獲取每個帖子及其類別的標簽? 在linq查詢中? (我嘗試向用戶顯示默認頁面中每個帖子的模板

這是我要在默認頁面中顯示給用戶的內容。 (清單)

像這樣的某人

如果您的Post類是實體類,則無需使用select創建另一個匿名類型。 下面的Linq鏈應該可以幫助您實現目標。 同樣根據注釋-列表返回null並不是一個好習慣,調用者可以檢查Count屬性以查看列表是否為空。

        using (var context = new MJBweblogContext())
        {
            return context.Posts.Where(post => post.Published)
                .OrderByDescending(p => p.PostedOn)
                .Skip(pageNo * pageSize)
                .Take(pageSize)
                .ToList();
        }

您不能只跳過所有其他內容嗎?

List<Post> IBlogRepository.PostsForList(int pageNo, int pageSize)
    {
        using (var context = new MJBweblogContext())
        {
            return context.Posts.Where(a => a.Published == true)
                                .OrderByDescending(p => p.PostedOn)
                                .Skip(pageNo * pageSize)
                                .Take(pageSize)
                                .ToList();
        }
    }

如果您沒有獲得類別信息,則應該可以使用INCLUDE

List<Post> IBlogRepository.PostsForList(int pageNo, int pageSize)
    {
        using (var context = new MJBweblogContext())
        {
            return context.Posts.Include("Category").Where(a => a.Published == true)
                                .OrderByDescending(p => p.PostedOn)
                                .Skip(pageNo * pageSize)
                                .Take(pageSize)
                                .ToList();
        }
    }

暫無
暫無

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

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