views:

40

answers:

1

Hi All,

I have the following query in Linq:

var query = from question in context.Questions.Include("QAnswers")
            join answer in context.Answers
                on question.id equals answer.qst
            where answer.user == param_userID
            select question;    
return query.toList();

The problem is that it doesn't load the "QAnswers" navigation property at all. Is there any way in Entity Framework to return the entity and restricted result set in its navigation property?

Ps. I'm using EF4 if it's important

A: 

If I understood it correctly, this should be enough:

context.Questions.Include("QAnswers").Where(q => q.QAnswers.Any(a => a.user == param_userID));

These are questions where specific user answered. There is no need for joins.

EDIT

context.Questions.Where(q => q.QAnswers.Any(a => a.user == param_userID)).Select(q => new { Question = q, Answers = q.QAnswers.Where(a => a.user == param_userID) }).ToList();

This will return question and only specific user's answers.

LukLed
Not really.Your method will return a questions that match the criteria but each question.QAnswers property will contains ALL available answers related to the particular question. The point is to have in nav property "question.QAnswers" only answers meeting condition a.user == param_userID
palcan