Solarion
Honorary Master
- Joined
- Nov 14, 2012
- Messages
- 28,051
- Reaction score
- 17,804
I have two tables, Authors and Books
On my program I have pulled the information into List<Authors> and List<Books> and now obviously an Author can have 1 or many Books
What I would like is to filter the Authors list where an Author has written 5 or more Books.
I've wasted over 2 hours trying to figure this out so I have one question, is this even possible at all with LINQ or Lamda's? I came across another suggestion that I must use IQueryable instead of IList.
Is this possible?
Edit: Before you say anything, yes I should and probably will put an AuthorId column in the Books table!
This is what I've got and it works, albeit in a roundabout way.
Code:
var uniquelist = from x in Books
group x by x.AuthorID into g
let count = g.Count()
orderby count descending
select new { Name = g.Key, Count = count, g.First().AuthorID};
var morethanfivebooks = q.Where(x => x.Count >= 5).ToList();
authors = authors.Where(x => morethanfivebooks.Any(y => y.AuthorID == x.AuthorID)).ToList();
What I'm left with is a list of authors who have written 5 or more books.
Last edited: