Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb linq Count Distinct?

Posted on 2016-09-27
1
Medium Priority
?
64 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
[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
  • Learn & ask questions
1 Comment
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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