LINQ newbie question [modified]
-
I have several tables;
(Table) Task
id
type_id
category_id(Table) Type
type_id
type_description(Table) Category
category_id
category_descriptionand 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
-
I have several tables;
(Table) Task
id
type_id
category_id(Table) Type
type_id
type_description(Table) Category
category_id
category_descriptionand 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
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.
-
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.
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
-
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
:-D That would do it. Sometimes crude is all you've got.
Deja View - the feeling that you've seen this post before.