我有一個像下面的模式(這是一個簡單的例子,所以請無視明顯的架構問題):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 <TSource>(System.Collections.Generic.IEnumerable <TSource>,TSource)' 不能從使用推斷。嘗試明確指定類型參數。
感謝您的建議。你可以讓我知道這兩個查詢之間的區別是什麼? – user682732 2011-03-29 20:01:18
@user,區別在於在你的查詢中你嵌套了子查詢,這比我的查詢可以一行一行讀取更難讀。我的第二個查詢實際上與Jon的答案中的最後一個查詢相同,但在使用Linq語法編寫查詢時使用'Enumerable'方法編寫。我會說,如果你喜歡Linq語法,你應該使用第二或第三個Jon的查詢,否則使用我的第二個查詢。 – Snowbear 2011-03-29 20:04:55