如何在ASP.NET MVC中连接多表时返回新表

时间:2018-04-20 04:57:20

标签: asp.net

public List<Book> getListBooks(int top)
    {
        var result = (from book in db.Books
                      join author in db.Authors on book.AuthorID equals author.ID
                      join publisher in db.Publishers on book.PublisherID equals publisher.ID
                      join category in db.BookCategories on book.CategoryID equals category.ID
                      join catalog in db.BookCatalogs on category.catalogID equals catalog.ID
                      where book.Status == true
                      select new 
                      {
                          ID = book.ID,
                          Name = book.Name,
                          Alias = book.Alias,
                          Price = book.Price,
                          Quanlity = book.Quality,
                          ViewCount = book.ViewCount,
                          Status = book.Status,
                          Image = book.Image,
                          MoreImages = book.MoreImages,
                          PublicationDate = book.PublicationDate,
                          BookCover = book.BookCover,
                          Catalog = catalog.Name,
                          Category = category.Name,
                          Publisher = publisher.Name,
                      });
        return result.ToList();
    }
  

严重级代码描述项目文件行抑制状态   错误CS0029无法隐式转换类型&#39; System.Collections.Generic.List&lt;&gt;&#39;到&#39; System.Collections.Generic.List&#39;型号

1 个答案:

答案 0 :(得分:0)

您无法将匿名类型直接转换为List<T>,而是必须指定类别/类型。

select new设置为select new Book进行转换。

public List<Book> getListBooks(int top)
{
    var result = (from book in db.Books
                  join author in db.Authors on book.AuthorID equals author.ID
                  join publisher in db.Publishers on book.PublisherID equals publisher.ID
                  join category in db.BookCategories on book.CategoryID equals category.ID
                  join catalog in db.BookCatalogs on category.catalogID equals catalog.ID
                  where book.Status == true
                  select new Book
                  {
                      ID = book.ID,
                      Name = book.Name,
                      Alias = book.Alias,
                      Price = book.Price,
                      Quanlity = book.Quality,
                      ViewCount = book.ViewCount,
                      Status = book.Status,
                      Image = book.Image,
                      MoreImages = book.MoreImages,
                      PublicationDate = book.PublicationDate,
                      BookCover = book.BookCover,
                      Catalog = catalog.Name,
                      Category = category.Name,
                      Publisher = publisher.Name,
                  });
    return result.ToList();
}

此外,您也可以这样做。

public List<Book> getListBooks(int top)
{
    var result = (from book in db.Books
                  join author in db.Authors on book.AuthorID equals author.ID
                  join publisher in db.Publishers on book.PublisherID equals publisher.ID
                  join category in db.BookCategories on book.CategoryID equals category.ID
                  join catalog in db.BookCatalogs on category.catalogID equals catalog.ID
                  where book.Status == true
                  select new
                  {
                      ID = book.ID,
                      Name = book.Name,
                      Alias = book.Alias,
                      Price = book.Price,
                      Quanlity = book.Quality,
                      ViewCount = book.ViewCount,
                      Status = book.Status,
                      Image = book.Image,
                      MoreImages = book.MoreImages,
                      PublicationDate = book.PublicationDate,
                      BookCover = book.BookCover,
                      Catalog = catalog.Name,
                      Category = category.Name,
                      Publisher = publisher.Name,
                    }).AsEnumerable().Select(x => new Book 
                    {
                      ID,
                      Name,
                      Alias,
                      Price,
                      Quanlity,
                      ViewCount,
                      Status,
                      Image,
                      MoreImages,
                      PublicationDate,
                      BookCover,
                      Catalog,
                      Category,
                      Publisher,
                    }).ToList();
return result.ToList();
}