Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

c# - Linq to Entities - eager loading using Include()

I've got this really basic table structure:

dbo.tblCategory
dbo.tblQuestion (many to one relationship to tblCategory)
dbo.tblAnswer (many to one relationship to tblQuestion)

So basically, what I'm trying to do is when I load a category, I want to also load all Questions, and all Answers.

Now, I've been able to do this using the following code:

public tblCategory Retrieve(int id)
{
    using (var entities = Context)
    {
        var dto =
            (from t in entities.tblCategory.Include("tblQuestion")
                                           .Include("tblQuestion.tblAnswers")    
                 where t.Id == id
                 select t).FirstOrDefault();

            return entities.DetachObjectGraph(dto);
        }
    }
}

However, I'm not completely enamored with this; if the relationship names change in my model; I'm not going to get a error when building the project. Ideally, I'd like to use a lambda expression; something like this:

public tblCategory Retrieve(int id)
{
    using (var entities = Context)
    {
        var dto =
            (from t in entities.tblCategory.Include(t => t.tblQuestion)
             where t.Id == id
             select t).FirstOrDefault();

        return entities.DetachObjectGraph(dto);
    }
}

Now, with the above snippet; I'm stuck on how to drill down to the Answers table. Any idea on what I could use for a lambda expression for this one?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK; I was able to get this to work, with some help from here.

Basically, I need to do this:

public tblCategory Retrieve(int id)
{
    using (var entities = Context)
    {
        var dto =
            (from t in entities.tblCategory.Include(t => t.tblQuestion)
                                           .Include(t => t.tblQuestion.First().tblAnswer)
             where t.Id == id
             select t).FirstOrDefault();

        return entities.DetachObjectGraph(dto);
    }
}

From the link above, I can only dereference tblAnswers on individual items of the questions collection. Here I chose to dereference tblAnswers on the first item of the collection. In reality, this lambda expression is merely used to produce the property path “tblQuestion.tblAnswers”, which will eager load the answers of all questions.

So even though it looks like I'm only pulling the answers for the first question, I'm actually pulling all the answers for all the questions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...