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. Union in LINQ

Union in LINQ

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqhelpquestion
3 Posts 2 Posters 2 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

    What is the criteria for a union in Linq. The criteria for SQL is that the select clumns must have the same name & amount of columns between the two tables. This is what I have & an error is thrown in linq. var rsPunch = (from a in PunchCtx.AccessPunches select new { a.1, a.2 }); var rsPunch1 = (from t in PunchCtx.TimeAtendancePunches select new { t.1, t.2 }); var union = rsPunch.Union(rsPunch1);

    H 1 Reply Last reply
    0
    • U u2envy12

      What is the criteria for a union in Linq. The criteria for SQL is that the select clumns must have the same name & amount of columns between the two tables. This is what I have & an error is thrown in linq. var rsPunch = (from a in PunchCtx.AccessPunches select new { a.1, a.2 }); var rsPunch1 = (from t in PunchCtx.TimeAtendancePunches select new { t.1, t.2 }); var union = rsPunch.Union(rsPunch1);

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      LINQ operates in a similar way to SQL but not exactly the same. To answer this try the following statements before the union:

      var t1 = rsPunch.GetType().ToString();
      var t2 = rsPunch1.GetType().ToString();

      You should get something like this:

      t1 = "System.Data.Linq.DataQuery`1[<>f__AnonymousType0`2[..data types..]]"
      t2 = "System.Data.Linq.DataQuery`1[<>f__AnonymousType1`2[..data types..]]"

      I hope you can see that the two queries acutally generate data queries of *different types*. The 'new {}' statement creates an anonymous type which is different each time, even though the types of the elements inside are the same.

      'Howard

      H 1 Reply Last reply
      0
      • H Howard Richards

        LINQ operates in a similar way to SQL but not exactly the same. To answer this try the following statements before the union:

        var t1 = rsPunch.GetType().ToString();
        var t2 = rsPunch1.GetType().ToString();

        You should get something like this:

        t1 = "System.Data.Linq.DataQuery`1[<>f__AnonymousType0`2[..data types..]]"
        t2 = "System.Data.Linq.DataQuery`1[<>f__AnonymousType1`2[..data types..]]"

        I hope you can see that the two queries acutally generate data queries of *different types*. The 'new {}' statement creates an anonymous type which is different each time, even though the types of the elements inside are the same.

        'Howard

        H Offline
        H Offline
        Howard Richards
        wrote on last edited by
        #3

        Oops I deleted the "how to fix bit". To union the results, ensure they create the same type of value. Perhaps create a struct or class than contains the two values and a constructor. Or you might be able to use a KeyValuePair<TKey, TValue> if that is easier. Then you should be able to use Union.

        'Howard

        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