2

I simply can not get this to work out at all, so any expert help would be very much appreciated.

I'm trying (as the subject suggests) to join 2 datatables on Zip Code, but return a table which grouped this by State and has a SUM() of sales.

Here's the latest version of my troubles:

var results =(
    from a in dtList.AsEnumerable()
    join b in dtListCoded.AsEnumerable()
    on a.Field<string>("ZIP") equals b.Field<string>("zip") 
    group a by {a.Field<string>("StateCode")} into g
    select new { 
       StateCode = a.Field<string>("StateCode"),
       SumSales = b.Sum(b => b.Field<double>("SUMSales"))
    });

I can join the 2 tables but its getting the result i need that seems to be the tricky bit. If need be I will just have to do 2 queries, but that just seems a bit backward.

Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CatchingMonkey
  • 1,391
  • 2
  • 14
  • 36
  • Hey, CatchingMonkey! I have a similar question. The answer by Kaido does not contain enough detail for me. My question is here: http://stackoverflow.com/q/43766663/1735836. Can you help me out? – Patricia May 03 '17 at 18:58

1 Answers1

2

Two queries wouldn't be any slower (they should be brought together into a single SQL query upon execution), and would be a lot more readable, transparent during debugging and reusable. I'd recommend breaking it down.

Kaido
  • 3,383
  • 24
  • 34
  • Hey, Kaido! Your answer does not contain enough detail for me. I have a similar question.here: stackoverflow.com/q/43766663/1735836. Can you help me out? – Patricia May 03 '17 at 19:00