I have an XML file that represents a list of books that the user has access to.
<Book Bookid="1" BookName="Book1" />
<Book Bookid="1" BookName="Book1" />
<Book Bookid="2" BookName="Book2" />
<Book Bookid="1" BookName="Book1" />
Dim DistinctBooks = (From item In xBooks.Elements("Book") Select item).Distinct()
Assuming this is declared:
Dim xBooks = <books>
<Book Bookid="1" BookName="Book1"/>
<Book Bookid="1" BookName="Book1"/>
<Book Bookid="2" BookName="Book2"/>
<Book Bookid="1" BookName="Book1"/>
</books>
I think you are looking for something like this:
Dim DistinctBooks = From item In xBooks.Elements("Book")
Select bookid = item.Attribute("Bookid").Value,
bookName = item.Attribute("BookName").Value
Distinct
Why it did not work for you is because every node is different if you consider a set of all properties that it implements. With the above you tell it which properties you need as part of a unique key for distinct. Also notice there is no need to combine multiple styles of LINQ (query and functional), you can use word Distinct
as part of the query.