views:

112

answers:

2

In straight SQL, I'd probably wrap some logic over a simple join, but how would I do this efficiently in LINQ to SQL? Imagine I have two tables:

Parent

  • ID

Child

  • ParentID
  • ID
  • Name

Ideally I'd like a graph of Parent objects with a "Childs" property that is actually a Dictionary<int, string>.

Suggestions?

+2  A: 
//set up dataloadoptions

DataLoadOptions options = new DataLoadOptions();

options.LoadWith<Parent>(item => item.Child);

context.LoadOptions = options;

var children = context.Parent.First().Child;
ravi
A: 

Something along the lines of this:

MyDataContext.Parents
    .Select(p => 
        new { p.ID, 
             Childs = p.Children.ToDictionary(c => c.ID, c => c.Name)
        }
     );

The above will project an anonymous type with two properties: An ID ( being the Parent's) and a Dictionary containing the childrens' ID and Name.

Andreas Grech