LINQ 分组依据和不在
我有一个如下所示的架构(这是一个简化的示例,因此请忽略明显的架构问题):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的嵌套查询正在选择匿名类型 - 您应该选择图书 ID:
但我会以更积极的方式表达它 - 实际上用两个单独的语句以使其更清晰:
编辑:或者,根据 Snowbear 的建议,使用join,我们将把它与查询延续一起使用以获得乐趣:
Your nested query is selecting an anonymous type - you should be selecting the book ID:
but I would express it in a more positive way - and actually with two separate statements to keep it clearer:
EDIT: Or, as per Snowbear's suggestion, using a join, which we'll use with a query continuation for fun:
您在嵌套查询中选择匿名类型,请将其替换为
select GroupedPerStudent.Key
:尽管我会重写整个查询:
You're selecting anonymous type in your nested query, replace it with
select GroupedPerStudent.Key
:Though I would rewrite the entire query: