0

What I would like to do is something I find common in MySQL which is to select all of one field distinct, and a sum of another field in that data sub-set for each unique/distinct result.

Following should replicate something simple to use in order to see the type of data I mean :

public class Stock {
   public int id {get;set;}
   public int count {get;set;}
   public string location {get;set;}
   public Stock() { }
}

public class Foo {

   public void Bar() {
        List<Stock> Stocks = new List<Stock>()
        {
            new Stock
            {
                id = 137829,
                count = 5,
                location = "Ottawa"
            },
            new Stock
            {
                id = 137829,
                count = 27,
                location = "Toronto"
            },
            new Stock
            {
                id = 137830,
                count = 8,
                location = "Toronto"
            }

        };

        var result = Stocks.DistincyBy( s => s.id);
   }
}

What I would like to store in result is as follows (using this code as an example. the real project the structure is more complex).

  • id = 137829, count = 32
  • id = 137830, count = 8

I know I can use .Sum(), but in this scenario I wish to sum where one field is distinct. How can this be achieved (i prefer my Linq to be inline syntax rather than sql style querying)

Kraang Prime
  • 9,981
  • 10
  • 58
  • 124

1 Answers1

2

Use GroupBy to get the distinct groupings, then Select into your objects. For example:

var result = Stocks.GroupBy(s => s.Id).Select(g => new 
{
   Id = g.Key,
   Count = g.Sum(i => i.Count)
};

Better would be to create a named class to select into, but the syntax would be very similar.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • How can this be used on dynamic enumerable ? `IEnumerable result = auction.GroupBy(s => s.item).Select(g => new { Id = g.Key, Count = g.Sum(i => i.buyout) };` results in a nesting issue where `i.item` doesn't exist. The error is: The name 'item' does not exist in the current context. – Kraang Prime Jul 12 '16 at 20:14
  • @SanuelJackson I have no idea. An `IEnumerable` sounds like a *really* bad idea to me, let alone summing one of its properties. – BradleyDotNET Jul 12 '16 at 20:17
  • `JavaScriptSerializer` decoding a large JSON data set ( 6 MB ). Wouldn't mind a fast way to convert the result reliably into a typed structure. The Linq works on the first part of the enumerable, but sub-linq querying can not access the enumerator with this code. – Kraang Prime Jul 12 '16 at 20:23
  • @SanuelJackson How about using JsonSerializer from Newtonsoft.Json? Then its all strongly typed and LINQ works as expected. – BradleyDotNET Jul 12 '16 at 20:26
  • I am aware of the Json projects out there and their pluses and pitfals. I am using a class that was written by another SO user which works perfectly. I am sure there is a way to access this data and query it and / or convert the data into a strong type. I am not looking for another project just to suit that it resolves one issue :) – Kraang Prime Jul 12 '16 at 20:32
  • @SanuelJackson Fair enough. In case you aren't aware, Newtonsoft is used by pretty much the entire ASP.NET world. I would personally trust it over code I found on SO. Good luck with your dynamic objects though. – BradleyDotNET Jul 12 '16 at 20:37
  • I am aware. If curious, http://stackoverflow.com/a/3806407/3504007 is the deserialization I am using. – Kraang Prime Jul 12 '16 at 20:48