不支持的聚合扩展方法(LINQ至SQL)[英] Aggregate Extension Method Not Supported (Linq to Sql)

本文是小编为大家收集整理的关于不支持的聚合扩展方法(LINQ至SQL)的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图在LINQ到SQL中使用聚合方法,但似乎不支持它.

LibrarySystemDataContext ctx = new LibrarySystemDataContext();

var query = ctx.Books
  .Select(x => new
      {
        BookID = x.BookID,
          BookName = x.Title,
          Authors = x.AuthorBooks.Select(a => a.Author)
          .Aggregate(new StringBuilder(), (sb, r) => sb.Append(r.FirstName + " " + r.LastName +"& "), sb => sb.ToString())
          });

要使用汇总,我立即将书籍转换为iEnumerable,如下:

var query = ctx.Books.AsEnumerable()
  .Select(x => new
      {
        BookID = x.BookID,
          BookName = x.Title,
          Authors = x.AuthorBooks.Select(a => a.Author)
          .Aggregate(new StringBuilder(), (sb, r) => sb.Append(r.FirstName + " " + r.LastName +"& "), sb => sb.ToString())
          });

,这正在起作用.我想知道,如果我写这样的查询,将是什么缺点或设计缺陷.

推荐答案

使用ctx.Books.AsEnumerable()您将从数据库中选择整本书表,然后开始在内存中处理它.这不是一个好设计.您应该做的是,选择所需的数据,然后在本地处理.

例如

var temp = ctx.Books
  // probably some where clause
  .Select(x => new
      {
        BookID = x.BookID,
          BookName = x.Title,
          Authors = x.AuthorBooks.Select(a => a.Author)
          })
  .ToArray();

var books = temp.Select(x => new { 
    x.BookID, 
    x.BookName, 
    Authors = x.Authors.Aggregate(new StringBuilder(), (sb, r) => sb.Append(r.FirstName + " " + r.LastName +"& "), sb => sb.ToString()) 
});

本文地址:https://www.itbaoku.cn/post/1557181.html

问题描述

I was trying to use Aggregate Method in linq to Sql but looks like it is not supported.

LibrarySystemDataContext ctx = new LibrarySystemDataContext();

var query = ctx.Books
  .Select(x => new
      {
        BookID = x.BookID,
          BookName = x.Title,
          Authors = x.AuthorBooks.Select(a => a.Author)
          .Aggregate(new StringBuilder(), (sb, r) => sb.Append(r.FirstName + " " + r.LastName +"& "), sb => sb.ToString())
          });

to use Aggregate, i immediately converted books to IEnumerable as follows:

var query = ctx.Books.AsEnumerable()
  .Select(x => new
      {
        BookID = x.BookID,
          BookName = x.Title,
          Authors = x.AuthorBooks.Select(a => a.Author)
          .Aggregate(new StringBuilder(), (sb, r) => sb.Append(r.FirstName + " " + r.LastName +"& "), sb => sb.ToString())
          });

And, this is working. I want to know what will be the drawbacks or design flaws if i write queries like this.

推荐答案

with ctx.Books.AsEnumerable() you will select the whole books table from database and start processing it in memory. This isn't good design. What you should do is, select the data you need and then process it locally.

for example

var temp = ctx.Books
  // probably some where clause
  .Select(x => new
      {
        BookID = x.BookID,
          BookName = x.Title,
          Authors = x.AuthorBooks.Select(a => a.Author)
          })
  .ToArray();

var books = temp.Select(x => new { 
    x.BookID, 
    x.BookName, 
    Authors = x.Authors.Aggregate(new StringBuilder(), (sb, r) => sb.Append(r.FirstName + " " + r.LastName +"& "), sb => sb.ToString()) 
});