Solved

vb linq Count Distinct?

Posted on 2016-09-27
1
41 Views
Last Modified: 2016-09-28
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()

Open in new window

0
Comment
Question by:Jess31
1 Comment
 
LVL 62

Accepted Solution

by:
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()

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now