0

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 Answers 2

2
//set up dataloadoptions

DataLoadOptions options = new DataLoadOptions();

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

context.LoadOptions = options;

var children = context.Parent.First().Child;
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.