EF6包含与连接不包含. [英] EF6 include with join not including.

查看:65
本文介绍了EF6包含与连接不包含.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很清楚,在更简单的情况下,这是一个明确解决方案的问题,但是我已经实施了一些我认为可以做到的事情或由于此问题而完全更改它是错误的,也许有些东西我看不到.好吧,我有客户,每个客户都有任务,每个任务都有代理分配给它.我想将所有内容加载到一个对象,并根据我从客户端获得的一些搜索对象来查询这些参数,就像这样.

I'm well aware this is an issue with a clear solution in more simple situations, but i have implemented something that i think doing it otherwise or changing it completely due to this issue is wrong, maybe there is something that I don't see. well, I have clients, each client have missions,each mission has agents assigned to it. I want to load all that to an object, and to query on those parameters according to some search object i get from my client, like this.

public IEnumerable<object> GetMissionsBySearch(MissionSearchCriteria search, int loggedUserId)
    {
        var today = DateTime.Now.AddHours(12);
       try
        {
            var query = from mission in context.Missions.Include("AssignedAgents")
                join client in context.Clients on mission.ClientId equals client.Id
                select new
                    {
                        mission,
                        client.Name,
                        client.Office,
                        client.Id,
                    }
                into x
                select x;

            if (search.ByType != ActivityType.All)
            {
                query = query.Where(s => s.mission.ActivityType == search.ByType);
            }
            if (search.IsTakenCareOf != "all")
            {
                bool endedOrNot;
                bool value = Boolean.TryParse(search.IsTakenCareOf, out endedOrNot);
                if (value)
                {
                    query = query.Where(s => s.mission.TakenCareOf == endedOrNot);    
                }

            }
            if (search.ByCreator == ByCreator.Me)
            {
                query = query.Where(s => s.mission.AddedbyUserId == loggedUserId
                    && s.mission.AssignedAgents.Count(a => a.Id == loggedUserId) > 0);
            }
            if (search.ByCreator == ByCreator.Other)
            {
                query = query.Where(s => s.mission.AddedbyUserId != loggedUserId 
                    && s.mission.AssignedAgents.Count(a => a.Id == loggedUserId) > 0);
            }
            if (search.ByCreator == ByCreator.Both)
            {
                //query = query.Where(s => s.mission.AddedbyUserId == loggedUserId
                //   || s.mission.AssignedAgents.Count(a => a.Id == loggedUserId) > 0);
                //query = query.Where(s => s.mission.AddedbyUserId != loggedUserId
                //   && s.mission.AssignedAgents.Count(a => a.Id == loggedUserId) > 0);
            }
            if (search.ByDate == ByDate.All)
            {
                //Doing Nothing
            }
            if (search.ByDate == ByDate.Today)
            {
                query = query.Where(s=> s.mission.Deadline.Value <= today);
            }
            else if (search.ByDate == ByDate.Yesterday)
            {

                var yesterday = DateTime.Now.AddHours(-24);
                query = query.Where(s => s.mission.Deadline > yesterday
                    && s.mission.Deadline < today);
            }
            else if (search.ByDate == ByDate.LastWeek)
            {
                var lastweek = DateTime.Now.AddDays(-8);
                query = query.Where(s => s.mission.Deadline > lastweek
                    && s.mission.Deadline < today);
            }
            else if (search.ByDate == ByDate.ByDays && search.Days > 0)
            {
                var daysBack = DateTime.Now.AddDays(search.Days*-1);
                query = query.Where(s => s.mission.Deadline > daysBack);
            }
            else if (search.ByDate == ByDate.BetweenDates)
            {
                query = query.Where(s => s.mission.Deadline > search.FromDate && s.mission.Deadline < search.ToDate);
            }
            else if (search.ByDate == ByDate.EveryThingBackToDate)
            {
                query = query.Where(s =>  s.mission.Deadline < search.ToDate);
            }              
            return query.ToList();

我阅读了这三篇文章,但仍然无法成功地使它起作用,实体框架&包含 http://itsharpau.wordpress.com/2011/09/30/entity-framework-4-query-with-include-doesnt-work-with-join/

I read those three articles and still have no success in getting this to work, Entity Framework & Include http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx http://itsharpau.wordpress.com/2011/09/30/entity-framework-4-query-with-include-doesnt-work-with-join/

请帮助,谢谢.

推荐答案

Include 永远无效.想一想,EF应该在哪个班级中包含子集合?

Include never works if you project to an (anonymous or named) type. Think of it, in which class should EF include the child collection?

如果包含,包含将起作用

The include will work if you do

var query = from mission in context.Missions.Include("AssignedAgents")
    join client in context.Clients on mission.ClientId equals client.Id
    select mission

然后EF将具有 Mission 对象,可以为其加载 AssignedAgents 集合.

Then EF will have Mission objects for which it can load AssignedAgents collections.

如果您不想获取完整的对象,则可以通过将 AssignedAgents 作为匿名类型的属性并入而获得相同的效果:

If you don't want to get the full objects you can get the same effect by incorporating AssignedAgents as a property in the anonymous type:

var query = from mission in context.Missions
    join client in context.Clients on mission.ClientId equals client.Id
    select new
        {
            mission,
            client.Name,
            client.Office,
            client.Id,
            Agents = mission.AssignedAgents
        };

这篇关于EF6包含与连接不包含.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆