Solved

vb linq Count Distinct?

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

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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