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 JOIN

LINQ JOIN

Scheduled Pinned Locked Moved C#
csharplinq
6 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.
  • P Offline
    P Offline
    PozzaVecia
    wrote on last edited by
    #1

    Hi, suppose I have

    var A = new[]
    {
    new {Name ="Car", N = 2},
    new {Name ="Bike", N = 100}
    };

            var B = new\[\]
            {
                new {Name ="Car", N = 10},
                new {Name ="Plane", N = 1}
            };
    

    I would like to to the following using LINQ syntax: Join A nd B getting a total collection summing also N where same objects are the same... to be clear the final output should be:

    {Name ="Car", N = 12},
    {Name ="Bike", N = 100},
    {Name ="Plane", N = 1}

    Thanks for your time

    M 1 Reply Last reply
    0
    • P PozzaVecia

      Hi, suppose I have

      var A = new[]
      {
      new {Name ="Car", N = 2},
      new {Name ="Bike", N = 100}
      };

              var B = new\[\]
              {
                  new {Name ="Car", N = 10},
                  new {Name ="Plane", N = 1}
              };
      

      I would like to to the following using LINQ syntax: Join A nd B getting a total collection summing also N where same objects are the same... to be clear the final output should be:

      {Name ="Car", N = 12},
      {Name ="Bike", N = 100},
      {Name ="Plane", N = 1}

      Thanks for your time

      M Offline
      M Offline
      Mirko1980
      wrote on last edited by
      #2

      Here you go:

      var query = from item in A.Union(B)
      group item by item.Name into grp
      select new { Name = grp.Key, N = grp.Sum(t => t.N) };

      modified on Wednesday, July 20, 2011 10:46 AM

      P 1 Reply Last reply
      0
      • M Mirko1980

        Here you go:

        var query = from item in A.Union(B)
        group item by item.Name into grp
        select new { Name = grp.Key, N = grp.Sum(t => t.N) };

        modified on Wednesday, July 20, 2011 10:46 AM

        P Offline
        P Offline
        PozzaVecia
        wrote on last edited by
        #3

        Suppose now I have many var. So not only two (A and B) but many, let's say A,B,C.... Is it still managable? (I mean a union between many and not anly two)

        P 1 Reply Last reply
        0
        • P PozzaVecia

          Suppose now I have many var. So not only two (A and B) but many, let's say A,B,C.... Is it still managable? (I mean a union between many and not anly two)

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Why not try it out and see what effect it has?

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          P 1 Reply Last reply
          0
          • P Pete OHanlon

            Why not try it out and see what effect it has?

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            P Offline
            P Offline
            PozzaVecia
            wrote on last edited by
            #5

            I'm only able to do union between two at time. My question is if I can do between many at the same time.

            P 1 Reply Last reply
            0
            • P PozzaVecia

              I'm only able to do union between two at time. My question is if I can do between many at the same time.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              And still I ask, why not try it out for yourself? You have the basic knowledge now, so it cannot be that hard for you to formulate your own opinion. Still, you can one vote this answer as well because I haven't spoonfed you the answer, rather I've suggested that you should now be able to think for yourself.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              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