5 Replies - 5240 Views - Last Post: 15 September 2010 - 06:15 AM Rate Topic: -----

#1 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

How to get reference to parent record in a LINQ sub query

Posted 14 September 2010 - 02:53 PM

Ok, I've run into a small challenge. I have this datatype:

public class Category
{
    public string Name { get; set; }
    public string FriendlyName { get; set; }
    public Category Parent { get; set; }
    public List<Category> Children { get; set; }
    public CategoryType Type { get; set; }
}

public enum CategoryType { Verb, Touchpoint };


With the idea being that each child in the Children list will have a reference to the parent in the Parent property.

Simple enough if I'm composing the lists separately, but I'd like to do this in a LINQ to SQL query.

Here's the query so far:

from x in tdc.Touchpoints
orderby x.Name
select new Category
{
    Parent = null,
    Name = x.Name,
    Type = CategoryType.Touchpoint,
    FriendlyName = x.Description,
    Children = (from y in tdc.Verbs
                where y.TouchpointId == x.Id
                orderby y.Name
                select new Category
                {
                    Parent = null,
                    Name = y.Name,
                    Type = CategoryType.Verb,
                    FriendlyName = y.Description,
                    Children = null
                }).ToList()
};


Line #14 is where I'd like to make a reference to the parent object, but I'm not entirely sure how I'd go about that. Any clues?

I could just query the two table separately and compose the list in memory, but now I'd like to know if it can be done this way.

Thanks.

This post has been edited by insertAlias: 14 September 2010 - 02:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to get reference to parent record in a LINQ sub query

#2 coultertech   User is offline

  • New D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 31
  • Joined: 18-May 09

Re: How to get reference to parent record in a LINQ sub query

Posted 14 September 2010 - 03:16 PM

How about

select new Category  
{  
     Parent = x,  
     Name = y.Name,  




Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to get reference to parent record in a LINQ sub query

Posted 14 September 2010 - 03:19 PM

Well, the parent isn't exactly "x". "x" is the object used to create the parent Category object, but "x" is a Touchpoint, not a Category.

And yes, I did use horrible names, it's an old habit from learning LINQ from examples that always used an "x".
Was This Post Helpful? 0
  • +
  • -

#4 coultertech   User is offline

  • New D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 31
  • Joined: 18-May 09

Re: How to get reference to parent record in a LINQ sub query

Posted 14 September 2010 - 04:26 PM

I don't think there is a way to do it in a single query. Since there is no object yet there is nothing to reference. So you're going to have to break it into two query, one to get the parents, then use a foreach loop through those parent and set the childrens list and you will have an object to set parent to. Would be cool if you could do that in a single statement though.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to get reference to parent record in a LINQ sub query

Posted 15 September 2010 - 04:26 AM

Is the Parent in line 05 a hardcoded value or something that will be populated from the collection?
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to get reference to parent record in a LINQ sub query

Posted 15 September 2010 - 06:15 AM

Parent in line 5 is hardcoded as null, because it's the top of the collection. This data structure is going to be bound to a Silverlight TreeView. The parent references aren't necessary for binding, but they are for later when a child element is selected and I have to find out what parent it belongs to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1