有一些问题。 需要按另一个集合对列表进行排序:
List<Books> b = new List<Books>();
课本:
public partial class Books
{
public Books()
{
this.BookAuthors = new HashSet<BookAuthors>();
}
public int BookID { get; set; }
public string BookName { get; set; }
public virtual ICollection<BookAuthors> BookAuthors { get; set; }
}
Class BookAuthors:
public partial class BookAuthors
{
public int Id { get; set; }
public int AuthorID { get; set; }
public int BookID { get; set; }
public virtual Books Books { get; set; }
public virtual Authors Authors { get; set; }
}
班级作者:
public partial class Authors
{
public Authors()
{
this.BookAuthors = new HashSet<BookAuthors>();
}
public int AuthorID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<BookAuthors> BookAuthors { get; set; }
}
我如何按作者姓氏排序b?
答案 0 :(得分:4)
这样的事情,虽然有多位作者,但仍然是一个问题,由你订购的作者:
var sorted =
from book in b
let firstAuthor = book.BookAuthors.First()
let lastName = firstAuthor.LastName
order book by lastName
select book
或者你可以应用一些逻辑(如果有的话)......
var sorted =
from book in b
let author = book.BookAuthors.FirstOrDefault(a=>a.Primary) ??
book.BookAuthors.FirstOrDefault(a=>a.Editor) ??
book.BookAuthors.First()
let lastName = author.LastName
order book by lastName
select book