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
800 views
in Technique[技术] by (71.8m points)

entity framework 4.1 - Include with projection does not work

I have this query

    var test = context.Assignments
        .Include(a => a.Customer)
        .Include(a => a.Subscriptions)
        .Select(a => new AssignmentWithSubscriptionCount { SubscriptionCount = a.Subscriptions.Count(), Assignment = a })
        .ToList();

    var name = test.First().Assignment.Customer.Name;

It failes to eagerly load Customer, I've seen similar problems here on stackoverflow and it looks like you cant use projections with include. But I have not found a solution to my problem.. Anyone?

edit: Here is a eager load with projection that work, its more complex than the example above so I cant for my life understand whats wrong, thanks.

    var test = context.PublicationStateGroups
        .Include(p => p.PublicationStates.Select(ps => ps.AllowedPublicationStateActions.Select(aps => aps.PublicationStateAction)))
        .Select(psg => new StateAndGroupInfo
        {
            ShowReport = psg.PublicationStates.Any(p => p.PublicationStateReportTypeId.HasValue),
            Actions = psg.PublicationStates.SelectMany(state => state.AllowedPublicationStateActions)
                .Select(a => a.PublicationStateAction)
                .Distinct()
        }).ToList();

    var eagerTest = test.First().Actions.First().Name;
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This is by design. Include is not for scenarios with projection or custom joins.


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

...