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. C#
  4. LINQ Query Grouping

LINQ Query Grouping

Scheduled Pinned Locked Moved C#
questioncsharpdatabaselinqsales
3 Posts 2 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.
  • E Offline
    E Offline
    eddieangel
    wrote on last edited by
    #1

    I have two tables, call it part and customer. Part PartId PartDesc 1 Bolt 2 Screw 3 Nut Customer CustomerId PartId 1 1 2 1 3 1 1 2 I want a result that looks like this: PartId CustomerId 1 1, 2, 3 Sorry for the rough example, but how do I flatten multiple records like that into a comma separated list? Thanks for any guidance, cheers, --EA

    OriginalGriffO 1 Reply Last reply
    0
    • E eddieangel

      I have two tables, call it part and customer. Part PartId PartDesc 1 Bolt 2 Screw 3 Nut Customer CustomerId PartId 1 1 2 1 3 1 1 2 I want a result that looks like this: PartId CustomerId 1 1, 2, 3 Sorry for the rough example, but how do I flatten multiple records like that into a comma separated list? Thanks for any guidance, cheers, --EA

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Try:

      SELECT PartId, CustomerIds=
      STUFF((SELECT ',' + CONVERT(VarChar(10), CustomerId)
      FROM Customer b
      WHERE b.PartId = a.PartId
      FOR XML PATH('')), 1, 1, '')
      FROM Customer a
      GROUP BY PartId

      Never underestimate the power of stupid things in large numbers --- Serious Sam

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      E 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Try:

        SELECT PartId, CustomerIds=
        STUFF((SELECT ',' + CONVERT(VarChar(10), CustomerId)
        FROM Customer b
        WHERE b.PartId = a.PartId
        FOR XML PATH('')), 1, 1, '')
        FROM Customer a
        GROUP BY PartId

        Never underestimate the power of stupid things in large numbers --- Serious Sam

        E Offline
        E Offline
        eddieangel
        wrote on last edited by
        #3

        I called this LINQ Query grouping in the title but probably should have specified this is a LINQ to Entities query. I think I have it figured out now, thank you though. Cheers, --EA

        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