Reputation: 1405
I have a datatable from some source which the structor
bookid title author
The table can contain the same book (repeated) if the book has several authors. Like
Book id title author
32 Book1 P. Samsom
45 Book34 R. Englund
45 Book34 H. Granger
56 Book45 F. Lister
etc.
Now the second and third book are the same ( it has 2 authors). When displaying the table (which can contain thousands items) I don't want to display the same book twice or more. It 's just enough with displaying one of them (I don't care).
Doing a Select distinct on the table won't help here. Doing a select distinct ignoring the author works, but I need at least ONE author. So what I want to do is to find the most effective way to just create a new table or view from this one which ignores any "duplicated" item keeping at least one author.
Any ideas?
Upvotes: 1
Views: 161
Reputation: 244
DistinctBy
from MoreLINQ
var results = books.DistinctBy(x => x.title).ToList();
Upvotes: 0
Reputation: 116108
var result = books.GroupBy(b => b.bookid)
.Select(g =>
new {
id = g.Key,
title = g.First().title,
authors = g.Select(a=>a.author).ToList()
})
.ToList();
Upvotes: 0
Reputation: 22555
var books = Books.GroupBy(x=>x.Title).Select(x=>x.First()).ToList();
Upvotes: 3