Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Grouping with LINQ

Grouping with LINQ

Scheduled Pinned Locked Moved LINQ
csharplinqquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    u2envy12
    wrote on last edited by
    #1

    This is what I have..... DailyHoursID 1 2 3 Table2 HoursTimeCategory DailyHoursID TimeCategory Hours Min 1 0 8 30 1 1 9 0 2 0 6 30 How do I get a result set of the bellow using LINQ TimeCategory Hours Min 0 14 60 1 9 0 Im getting the Hours Summed up. I just cant get the Minutes to sum up. var rsDailyHoursTC = (from d in Empctx.DailyHoursTimeCategories join tc in Empctx.TimeCategories on d.TimeCategoryID equals tc.TimeCategoryID where empCatList.Contains(d.DailyHoursID) group d.Hours by tc.TimeCategoryName into TotalArea select new { Category = TotalArea.Key, Hours = TotalArea.Sum() });

    A J 2 Replies Last reply
    0
    • U u2envy12

      This is what I have..... DailyHoursID 1 2 3 Table2 HoursTimeCategory DailyHoursID TimeCategory Hours Min 1 0 8 30 1 1 9 0 2 0 6 30 How do I get a result set of the bellow using LINQ TimeCategory Hours Min 0 14 60 1 9 0 Im getting the Hours Summed up. I just cant get the Minutes to sum up. var rsDailyHoursTC = (from d in Empctx.DailyHoursTimeCategories join tc in Empctx.TimeCategories on d.TimeCategoryID equals tc.TimeCategoryID where empCatList.Contains(d.DailyHoursID) group d.Hours by tc.TimeCategoryName into TotalArea select new { Category = TotalArea.Key, Hours = TotalArea.Sum() });

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      See if this works (its not tested). I am guessing you need things grouped by the TimeCategory.

      var rsDailyHoursTC = (from d in Empctx.DailyHoursTimeCategories
      group d.TimeCategory into TotalArea
      orderby TotalArea.TimeCategory
      select new
      {
      TimeCategory = TotalArea.TimeCategory,
      Hours = TotalArea.Sum( h => h.Hours),
      Min = TotalArea.Sum( m => m.Min)
      });

      EDIT: Changed from DailyHoursID to TimeCategory

      U 1 Reply Last reply
      0
      • A ABitSmart

        See if this works (its not tested). I am guessing you need things grouped by the TimeCategory.

        var rsDailyHoursTC = (from d in Empctx.DailyHoursTimeCategories
        group d.TimeCategory into TotalArea
        orderby TotalArea.TimeCategory
        select new
        {
        TimeCategory = TotalArea.TimeCategory,
        Hours = TotalArea.Sum( h => h.Hours),
        Min = TotalArea.Sum( m => m.Min)
        });

        EDIT: Changed from DailyHoursID to TimeCategory

        U Offline
        U Offline
        u2envy12
        wrote on last edited by
        #3

        You missing a by clause group by lambada Error on TotalArea.TimeCategory, var rsDailyMinutesTC1 = (from d in Empctx.DailyHoursTimeCategories join tc in Empctx.TimeCategories on d.TimeCategoryID equals tc.TimeCategoryID where empCatList.Contains(d.DailyHoursID) group d.TimeCategory by tc.TimeCategoryName into TotalArea select new { TimeCategory = TotalArea.TimeCategory, Hours = TotalArea.Sum( h => h.Hours), Min = TotalArea.Sum( m => m.Min) });

        A 1 Reply Last reply
        0
        • U u2envy12

          You missing a by clause group by lambada Error on TotalArea.TimeCategory, var rsDailyMinutesTC1 = (from d in Empctx.DailyHoursTimeCategories join tc in Empctx.TimeCategories on d.TimeCategoryID equals tc.TimeCategoryID where empCatList.Contains(d.DailyHoursID) group d.TimeCategory by tc.TimeCategoryName into TotalArea select new { TimeCategory = TotalArea.TimeCategory, Hours = TotalArea.Sum( h => h.Hours), Min = TotalArea.Sum( m => m.Min) });

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #4

          u2envy12 wrote:

          ambada Error on TotalArea.TimeCategory

          Is the column name TimeCategory or TimeCategoryName?

          var rsDailyHoursTC = (from d in Empctx.DailyHoursTimeCategories
          group d by d.TimeCategory into TotalArea
          orderby TotalArea.TimeCategoryName
          select new
          {
          TimeCategory = TotalArea.TimeCategoryName,
          Hours = TotalArea.Sum( h => h.Hours),
          Min = TotalArea.Sum( m => m.Min)
          });

          U 1 Reply Last reply
          0
          • A ABitSmart

            u2envy12 wrote:

            ambada Error on TotalArea.TimeCategory

            Is the column name TimeCategory or TimeCategoryName?

            var rsDailyHoursTC = (from d in Empctx.DailyHoursTimeCategories
            group d by d.TimeCategory into TotalArea
            orderby TotalArea.TimeCategoryName
            select new
            {
            TimeCategory = TotalArea.TimeCategoryName,
            Hours = TotalArea.Sum( h => h.Hours),
            Min = TotalArea.Sum( m => m.Min)
            });

            U Offline
            U Offline
            u2envy12
            wrote on last edited by
            #5

            Its TimeCategoryName Error on orderby TotalArea."TimeCategoryName" Does not contain a definition for TimeCategoryName

            A 1 Reply Last reply
            0
            • U u2envy12

              Its TimeCategoryName Error on orderby TotalArea."TimeCategoryName" Does not contain a definition for TimeCategoryName

              A Offline
              A Offline
              ABitSmart
              wrote on last edited by
              #6

              wht about the last query i gave you ??

              U 1 Reply Last reply
              0
              • A ABitSmart

                wht about the last query i gave you ??

                U Offline
                U Offline
                u2envy12
                wrote on last edited by
                #7

                Get an error on orderby TotalArea.TimeCategoryName If I remove orderby Get an error on TimeCategory = TotalArea.TimeCategoryName, var rsDailyHoursTC1 = (from d in Empctx.DailyHoursTimeCategories join tc in Empctx.TimeCategories on d.TimeCategoryID equals tc.TimeCategoryID where empCatList.Contains(d.DailyHoursID) group d by tc.TimeCategoryName into TotalArea orderby TotalArea.TimeCategoryName select new { TimeCategory = TotalArea.TimeCategoryName, Hours = TotalArea.Sum(h => h.Hours), Min = TotalArea.Sum(m => m.Minutes) });

                A 1 Reply Last reply
                0
                • U u2envy12

                  Get an error on orderby TotalArea.TimeCategoryName If I remove orderby Get an error on TimeCategory = TotalArea.TimeCategoryName, var rsDailyHoursTC1 = (from d in Empctx.DailyHoursTimeCategories join tc in Empctx.TimeCategories on d.TimeCategoryID equals tc.TimeCategoryID where empCatList.Contains(d.DailyHoursID) group d by tc.TimeCategoryName into TotalArea orderby TotalArea.TimeCategoryName select new { TimeCategory = TotalArea.TimeCategoryName, Hours = TotalArea.Sum(h => h.Hours), Min = TotalArea.Sum(m => m.Minutes) });

                  A Offline
                  A Offline
                  ABitSmart
                  wrote on last edited by
                  #8

                  u2envy12 wrote:

                  DailyHoursID 1 2 3 Table2 HoursTimeCategory DailyHoursID TimeCategory Hours Min 1 0 8 30 1 1 9 0 2 0 6 30

                  I wrote the query based on the data you provided. The query you have posted and data(tablename, column name) do not match at all.

                  U 1 Reply Last reply
                  0
                  • A ABitSmart

                    u2envy12 wrote:

                    DailyHoursID 1 2 3 Table2 HoursTimeCategory DailyHoursID TimeCategory Hours Min 1 0 8 30 1 1 9 0 2 0 6 30

                    I wrote the query based on the data you provided. The query you have posted and data(tablename, column name) do not match at all.

                    U Offline
                    U Offline
                    u2envy12
                    wrote on last edited by
                    #9

                    Sorry..... Let me get the names correct. DailyHoursID 1 2 3 Table2 HoursTimeCategory DailyHoursID TimeCategoryID Hours Min 1 0 8 30 1 1 9 0 2 0 6 30 Table3 TimeCategory TimeCategoryID TimeCategoryName 1 Normal Time 2 Over Time Join on Table3 to get the TimeCategoryName

                    1 Reply Last reply
                    0
                    • U u2envy12

                      This is what I have..... DailyHoursID 1 2 3 Table2 HoursTimeCategory DailyHoursID TimeCategory Hours Min 1 0 8 30 1 1 9 0 2 0 6 30 How do I get a result set of the bellow using LINQ TimeCategory Hours Min 0 14 60 1 9 0 Im getting the Hours Summed up. I just cant get the Minutes to sum up. var rsDailyHoursTC = (from d in Empctx.DailyHoursTimeCategories join tc in Empctx.TimeCategories on d.TimeCategoryID equals tc.TimeCategoryID where empCatList.Contains(d.DailyHoursID) group d.Hours by tc.TimeCategoryName into TotalArea select new { Category = TotalArea.Key, Hours = TotalArea.Sum() });

                      J Offline
                      J Offline
                      Jon Rista
                      wrote on last edited by
                      #10

                      You are trying to group and join at the same time, which is actually quite problematic in LINQ. Instead, join and select the fields you need into a new result set, then group that result set. Like so:

                      var rsDailyHoursTC =
                      from rs in
                      (
                      from d in Empctx.DailyHoursTimeCategories
                      join tc in Empctx.TimeCategories on d.TimeCategoryID equals tc.TimeCategoryID
                      where empCatList.Contains(d.DailyHoursID)
                      select new { TimeCategoryName = tc.TimeCategoryName, Hours = d.Hours, Minutes = d.Min }
                      )
                      group rs by rs.TimeCategoryName into g
                      select new { Category = g.Key, Hours = g.Sum(r => r.Hours), Minutes = g.Sum(r => r.Minutes) };

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups