LINQ 分组依据和不在

发布于 2024-10-27 17:39:15 字数 778 浏览 1 评论 0原文

我有一个如下所示的架构(这是一个简化的示例,因此请忽略明显的架构问题):

table Books
{
    string bookId
}

table students_books
{
    string studentname
    string bookId
}

目的是找出阅读次数少于 500 次的书籍。不幸的是,我无法将这个数字保留在图书表中。

我正在编写这样的查询:

from book in Books
    where !(from student in students_books
        group student by student.bookId into GroupedPerStudent
        where GroupedPerStudent.Count() >= 500 
        select new { bookname = GroupedPerStudent.Key }).Contains(book.bookid)
    select book 

我在 Contains() 上收到编译错误。查询出了什么问题?

无法从用法中推断出方法“System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable, TSource)”的类型参数。尝试显式指定类型参数。

I have a schema like below (it's a simplified example so please ignore the obvious schema issues):

table Books
{
    string bookId
}

table students_books
{
    string studentname
    string bookId
}

The intent is to find out books read less than 500 times. Unfortunately I cannot keep that count in the books table.

I am writing a query like this:

from book in Books
    where !(from student in students_books
        group student by student.bookId into GroupedPerStudent
        where GroupedPerStudent.Count() >= 500 
        select new { bookname = GroupedPerStudent.Key }).Contains(book.bookid)
    select book 

I am getting a compilation error on Contains(). What is wrong with the query?

The type arguments for method 'System.Linq.Enumerable.Contains<TSource>(System.Collections.Generic.IEnumerable<TSource>, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-11-03 17:39:15

您的嵌套查询正在选择匿名类型 - 您应该选择图书 ID:

from book in Books
  where !(from student in students_books
          group student by student.bookId into GroupedPerStudent
          where GroupedPerStudent.Count() >= 500 
          select GroupedPerStudent.Key).Contains(book.bookid)
select book;

但我会以更积极的方式表达它 - 实际上用两个单独的语句以使其更清晰:

var rarelyRead = from student in student_books
                 group student by student.bookId into grouped
                 where grouped.Count() < 5000
                 select grouped.Key;

var query = Books.Where(book => rarelyRead.Contains(book.book_id));

编辑:或者,根据 Snowbear 的建议,使用join,我们将把它与查询延续一起使用以获得乐趣:

var query = from student in student_books
            group student by student.bookId into grouped
            where grouped.Count() < 5000
            select grouped.Key into rarelyRead
            join book in Books on rarelyRead equals book.book_id
            select book;

Your nested query is selecting an anonymous type - you should be selecting the book ID:

from book in Books
  where !(from student in students_books
          group student by student.bookId into GroupedPerStudent
          where GroupedPerStudent.Count() >= 500 
          select GroupedPerStudent.Key).Contains(book.bookid)
select book;

but I would express it in a more positive way - and actually with two separate statements to keep it clearer:

var rarelyRead = from student in student_books
                 group student by student.bookId into grouped
                 where grouped.Count() < 5000
                 select grouped.Key;

var query = Books.Where(book => rarelyRead.Contains(book.book_id));

EDIT: Or, as per Snowbear's suggestion, using a join, which we'll use with a query continuation for fun:

var query = from student in student_books
            group student by student.bookId into grouped
            where grouped.Count() < 5000
            select grouped.Key into rarelyRead
            join book in Books on rarelyRead equals book.book_id
            select book;
单身狗的梦 2024-11-03 17:39:15

您在嵌套查询中选择匿名类型,请将其替换为 select GroupedPerStudent.Key

   ...
   where GroupedPerStudent.Count() >= 500 
         select GroupedPerStudent.Key).Contains(book.bookid)

尽管我会重写整个查询:

var popularBooks = students_books
                      .GroupBy(b => b.bookId)
                      .Where(g => g.Count() >= 500)
                      .Join(Books, students_book => students_book.Key, b => b.bookId,
                                  (students_books, book) => book);

You're selecting anonymous type in your nested query, replace it with select GroupedPerStudent.Key:

   ...
   where GroupedPerStudent.Count() >= 500 
         select GroupedPerStudent.Key).Contains(book.bookid)

Though I would rewrite the entire query:

var popularBooks = students_books
                      .GroupBy(b => b.bookId)
                      .Where(g => g.Count() >= 500)
                      .Join(Books, students_book => students_book.Key, b => b.bookId,
                                  (students_books, book) => book);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文