如何合并两个对象Class和Generic List <t>使用LINQ

时间:2015-10-28 13:35:42

标签: c# linq

如何根据以下示例合并两个对象Class和Generic List?

public class BookShop
{
    public Author Author { get; set; }
    public List<Book> Book { get; set; }
}

public class Author
{
    public string ID { get; set; }
    public string Name { get; set; }
}

public class Book
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class BookDetail
{
    public int ID { get; set; }
    public string Comment { get; set; }
    public int BookID { get; set; }
}

List<BookShop> shop = db.GetBook();
List<BookDetail> bookDetail = db.GetBookDetail();

var merge = shop.Select(m => m.Book.Join(bookDetail, ok => ok.ID, ik => ik.BookID, (b, d) => new
{
    b.ID,
    b.Name,
    d.Comment
}));

到目前为止一切都还可以,但我需要作者详细信息(作者姓名)

1 个答案:

答案 0 :(得分:1)

看起来你正在使用实体框架。您的结构看起来应该更像这样:

Uncaught TypeError: Cannot read property 'loadAssignmentsForList' of undefined

这使您可以直接连接书籍,作者和书籍详细信息。然后在Linq你可以做这样的事情来获得一本书的详细信息

public class Author
{
    public string ID { get; set; }
    public string Name { get; set; }
    public virtual IEnumerable<Book> Books { get; set; } 
}

public class Book
{
    public int ID { get; set; }
    public string Name { get; set; }

    public int AuthorID { get; set; }
    public Author Author { get; set; }
    public IEnumerable<BookDetail> Details { get; set; }
}

public class BookDetail
{
    public int ID { get; set; }
    public string Comment { get; set; }
    public int BookID { get; set; }
    public Book Book { get; set; }
}