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. LINQ newbie question [modified]

LINQ newbie question [modified]

Scheduled Pinned Locked Moved LINQ
databasequestioncsharplinqcom
4 Posts 2 Posters 3 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.
  • Mike HankeyM Offline
    Mike HankeyM Offline
    Mike Hankey
    wrote on last edited by
    #1

    I have several tables;

    (Table) Task
    id
    type_id
    category_id

    (Table) Type
    type_id
    type_description

    (Table) Category
    category_id
    category_description

    and I am issuing the following linq command

    var query
    from t in db.Task
    join tt in db.Type on t.type_id equals tt.type_id
    join tc in db.Category on t.category_id equals tc.category_id
    select new { t.id, tt.type_description, tc.category_description };

    What I'm ending up with is all records where ID's match and not the ones where they don't. I think what I want to do is some kind of outer join but I'm real confused as to what I need to do? I don't know SQl real well so I'm doubly dumb? Any help appreciated, Mike

    Semper Fi http://www.hq4thmarinescomm.com[^] My Site

    modified on Tuesday, September 30, 2008 10:48 PM

    P 1 Reply Last reply
    0
    • Mike HankeyM Mike Hankey

      I have several tables;

      (Table) Task
      id
      type_id
      category_id

      (Table) Type
      type_id
      type_description

      (Table) Category
      category_id
      category_description

      and I am issuing the following linq command

      var query
      from t in db.Task
      join tt in db.Type on t.type_id equals tt.type_id
      join tc in db.Category on t.category_id equals tc.category_id
      select new { t.id, tt.type_description, tc.category_description };

      What I'm ending up with is all records where ID's match and not the ones where they don't. I think what I want to do is some kind of outer join but I'm real confused as to what I need to do? I don't know SQl real well so I'm doubly dumb? Any help appreciated, Mike

      Semper Fi http://www.hq4thmarinescomm.com[^] My Site

      modified on Tuesday, September 30, 2008 10:48 PM

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

      You would normally use the DefaultIfEmpty operator to accomplish this. You have to take care to handle the null operator in the select.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles | MoXAML PowerToys

      Mike HankeyM 1 Reply Last reply
      0
      • P Pete OHanlon

        You would normally use the DefaultIfEmpty operator to accomplish this. You have to take care to handle the null operator in the select.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles | MoXAML PowerToys

        Mike HankeyM Offline
        Mike HankeyM Offline
        Mike Hankey
        wrote on last edited by
        #3

        Pete, Thanks got it working. What I was having problems with was the syntax. What I ended up with was pretty crude but works.

                var problems =
                   from p in db.Tasks
                   join pr in db.Products on p.product\_id equals pr.product\_id into g
                   from p2 in g.DefaultIfEmpty()
                   join pcc in db.TaskCategories on p.task\_category\_id equals pcc.task\_category\_id into f
                   from p3 in f.DefaultIfEmpty()
                   join pcs in db.TaskStatus on p.task\_status\_id equals pcs.task\_status\_id into j
                   from p4 in j.DefaultIfEmpty()
                   join pt in db.TaskTypes on p.task\_type\_id equals pt.task\_type\_id into k
                   from p5 in k.DefaultIfEmpty()
                   select new { p.task\_priority, p.task\_id, p.task\_name, p2.product\_description, p3.task\_category\_description, p4.task\_status\_description, p5.task\_type\_description };
        

        Thanks, Mike

        Semper Fi http://www.hq4thmarinescomm.com[^] My Site

        P 1 Reply Last reply
        0
        • Mike HankeyM Mike Hankey

          Pete, Thanks got it working. What I was having problems with was the syntax. What I ended up with was pretty crude but works.

                  var problems =
                     from p in db.Tasks
                     join pr in db.Products on p.product\_id equals pr.product\_id into g
                     from p2 in g.DefaultIfEmpty()
                     join pcc in db.TaskCategories on p.task\_category\_id equals pcc.task\_category\_id into f
                     from p3 in f.DefaultIfEmpty()
                     join pcs in db.TaskStatus on p.task\_status\_id equals pcs.task\_status\_id into j
                     from p4 in j.DefaultIfEmpty()
                     join pt in db.TaskTypes on p.task\_type\_id equals pt.task\_type\_id into k
                     from p5 in k.DefaultIfEmpty()
                     select new { p.task\_priority, p.task\_id, p.task\_name, p2.product\_description, p3.task\_category\_description, p4.task\_status\_description, p5.task\_type\_description };
          

          Thanks, Mike

          Semper Fi http://www.hq4thmarinescomm.com[^] My Site

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

          :-D That would do it. Sometimes crude is all you've got.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles | MoXAML PowerToys

          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