Solved

vb linq Count Distinct?

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

624 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