使用linq选择参考表的特定字段 [英] Select specific field of reference table using linq
本文介绍了使用linq选择参考表的特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
i有书桌,每本书都有很多章。
i为我的实体创建以下课程
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只想选择我章节的特定字段(在这个例子中只有chapterName)
我尝试过:
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
推荐答案
我发现解决方案
i find solution
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.
这篇关于使用linq选择参考表的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文