Solved

# vb linq Count Distinct?

Posted on 2016-09-27
50 Views
In the code bellow the NumberOfBooks does not return a count of distinct ISBNs. (If I have say 100 and 40 of them are the same ISBN I get a count of 100.) How can I have it return a count that would be based on Unique ISBN?

``````Dim results = (From r In dt.AsEnumerable()
Group By KeyEventArgs = "OneGroup" Into justOneGroup = Group
Select New With {
.NumberOfBooks = justOneGroup.Distinct.Count(Function(Books) Books.Field(Of String)("isbn")),
.CostPrice = justOneGroup.Sum(Function(c) c.Field(Of Decimal)("CostPriceTotal")),
.Qty = justOneGroup.Sum(Function(Books) Books.Field(Of Int32)("QtyLeft")),
.MaxQty = justOneGroup.Max(Function(Books) Books.Field(Of Int32)("MaxQty")),
.MinDate = justOneGroup.Min(Function(Books) Books.Field(Of Date)("FinalShipDate"))
}).ToList()
``````
0
Question by:Jess31
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
1 Comment

LVL 63

Accepted Solution

Fernando Soto earned 500 total points
ID: 41818917
Hi Jess31;

Try it like this
``````Dim results = (From r In dt.AsEnumerable()
Group By KeyEventArgs = "OneGroup" Into justOneGroup = Group
Select New With {
.NumberOfBooks = justOneGroup.Select(Function(Book) Book.Field(Of String)("isbn")).Distinct().Count(),
.CostPrice = justOneGroup.Sum(Function(c) c.Field(Of Decimal)("CostPriceTotal")),
.Qty = justOneGroup.Sum(Function(Books) Books.Field(Of Int32)("QtyLeft")),
.MaxQty = justOneGroup.Max(Function(Books) Books.Field(Of Int32)("MaxQty")),
.MinDate = justOneGroup.Min(Function(Books) Books.Field(Of Date)("FinalShipDate"))
}).ToList()
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Simple Linear Regression
Why Skyport?
###### Suggested Courses
Course of the Month4 days, 7 hours left to enroll