Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
i have book table and each book have many chapter
i create below class for my entity

public class Book
    {
        [Key]
        public int BookID { get; set; }

        [MinLength(10,ErrorMessage = "ISBN Minimum size should be 10"),MaxLength(13,ErrorMessage = "Maximum Lenght of ISBN should be 13 ")]
        public string ISBN { get; set; }
        public string bookName { get; set; }
        public string bookSummery { get; set; }
        public string bookAuthor { get; set; }

        public string bookTranslator { get; set; }
        
    
        public virtual ICollection<Chapter> Chapters { get; set;}

    }


 public class Chapter
    {
        public int ChapterId { get; set; }

        public string chapterName { get; set; }

        [ForeignKey("Book")]
        public int BookRefId { get; set; }
        public virtual Book Book { get; set; }
    }


i want to just select specific field of my chapter(in this exampe only chapterName)

What I have tried:

<pre>var _books = (from _b in db.Books
                          select new
                          {
                              _b.ISBN,
                              _b.BookID,
                             _b.bookName,
                             _b.bookSummery,
                             _b.Chapters = _b.Chapters.Select(details => new {
             
                                  details.chapterName,
                                 
                              },
                          });


but this code dont work for me
Posted
Updated 5-Feb-17 18:54pm
Comments
[no name] 5-Feb-17 12:13pm    
"don't work for me" means what exactly?
AminMhmdi 6-Feb-17 0:19am    
it say The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
Maciej Los 5-Feb-17 14:56pm    
Seems, you want to get books and chapters names. If i'm not wrong, you have to use SelectMany instruction.

1 solution

i find solution
C#
var _books = (from _b in db.Books
                         select new
                         {
                             _b.bookAuthor,
                             _b.bookCountry,
                             _b.BookID,
                             _b.bookName,
                             _b.bookTranslator,
                             _b.bookImage,
                             _b.bookPrice,
                             _b.bookTotalTime,
                             _b.Category,
                             chapters = from _ch in _b.Chapters
                                        select new
                                        {
                                            _ch.ChapterId,
                                           _ch.chapterName

                                        }
                         });


this solve me problem.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900