Solved

vb linq Count Distinct?

Posted on 2016-09-27
1
35 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

18 Experts available now in Live!

Get 1:1 Help Now