views:

34

answers:

2

I have a Parent/Child Table relationship with the ChildTable linking back to the ParentTable via the ParentId foreign key. There's multiple records in the ChildTable for each ParentTable record. For those of use you like rudimentary visuals, here's the diagram.

ParentTable
------------
+Id
Date

ChildTable
------------
+Id
ParentId
Date

What I'm trying to do is return only ONE record for each ParentTable item that's joined to the most recent ChildTable's date value (Note, not the Parent's Date value). The results would look like:

ParentTable::Id ParentTable::Foo ChildTable:Id ChildData::Foo ChildData::Date
--------------- ---------------- ------------- -------------- ---------------
55              Other Values     700           Other values   12/1/2010      
1               "                1000          "              11/30/2010
10              "                214           "              10/31/2010

It's important that the ChildData::Date is sorted descending.

This seems like it should be simple, but I'm struggling with the LinQ2SQL aspects of it. I'm still waiting for my "ah-ha" moment when I can think in Linq.

Here's the final answer, thanks to Winston:

var results = 
    from p in parent
    join c in (from c2 in child orderby c2.Date descending select c2)
    on p.Id equals c.ParentId into childGroup
    select new { ParentId=p.Id, ParentDate=p.Date, ChildDate=childGroup.First().Date } into NewResult
    orderby NewResult.Activity descending
    select NewResult
+1  A: 

The following should work, I've tested it on some dummy data.

var results = 
    from p in parent
    join c in (from c2 in child orderby c2.Date descending select c2)
    on p.Id equals c.ParentId into childGroup
    select new { ParentId=p.Id, ParentDate=p.Date, ChildDate=childGroup.First().Date };

Updated to select only the fields required.

Winston Smith
Ahhh..thanks Winston for editing. I was just getting ready to askk some questions.
taudep
I think I have it working. IT seems to be making a select for each ROW in the results. I'm wondering if there's a way to optimize that out? I might be returning 100+ rows of this data.
taudep
Actually, it's not quite working. If the Child table has TWO items for a given PARENT, it's returning both of them. Where does the uniqueness happen?
taudep
`Child = childGroup.First()` translates to a `SELECT TOP (1)`.
Winston Smith
"IT seems to be making a select for each ROW in the results". Selecting only the fields you want, instead of the entire objects eg `select new { ParentId=p.Id, ParentDate=p.Date, ChildDate=childGroup.First().Date }`
Winston Smith
Ahh, OK, so what I'm actually now having to do is only return the UNIQUE Parent record for the results. If there's multiple CHILD record for a given PARENT, it's returning multiple of the SAME PARENT objects, each with the same most recent CHILD object. Almost there!
taudep
Are you sure you have copied exactly as above? It should translate to a `select from parent` with an inner `select top (1) from child` for each row. Unless you have multiple parent records with the same id, I can't see how you can end up with multiple parent rows.
Winston Smith
Ahh, yes, I see we pulled out the "from child in childGroup" at some point (and I missed that in the latest iteration).
taudep
This returns the correct results. I just need to have the final results sorted by ChildDate, descending. Thanks so much for your help.
taudep
This seems to work: I've modified the end of the query to be: select new {Parent......} into foo, orderby foo.Activity descending, select foo.
taudep
A: 

Try something like

var x = ParentTable.Select(z=> new {
z.Id,
z.Foo,
z.ParentIDChildTable.OrderBy(c=> c.Date).First().ID,
z.ParentIDChildTable.OrderBy(c=> c.Date).First().Foo,
z.ParentIDChildTable.OrderBy(c=> c.Date).First().Date

}

There is possibly a better way to order the Child table before selecting but i can't think of it... Also the foreign key may not be as typed above but intellisense should help you there.

Si Robinson
For this, should ParentIdChildtable be that Child Object, a reference more like z.Child.First().ParendId? (I'm not understanding the context of the ParendIDChildTable)
taudep
The syntax assumes you have a foreign key relationship setup in SQL. You can then directly access that relational table. ParentIDChildTable is representative of that foreign key relationship.
Si Robinson
I like seeing this alternative approach. Thanks. ONe of the things that I continue to struggle with in Linq2Sql is the MULTIPLE ways of doing the same thing. Everyone develops their own "style" so to speak. Thanks.
taudep