我很清楚这是一个在更简单的情况下有明确解决方案的问题, 但我已经实现了一些我认为不这样做的东西 或者由于这个问题完全改变它是错误的, 也许有一些我看不到的东西。 好吧,我有客户,每个客户都有任务,每个任务都有代理人分配给它。 我想将所有这些加载到一个对象,并根据我从我的客户端获得的一些搜索对象查询这些参数,就像这样。
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();
我阅读了这三篇文章但仍未成功实现这一目标, 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/
请帮忙, 谢谢。
答案 0 :(得分:1)
Include
永远不会有效。想一想,哪个类应该EF包含子集合?
如果你这样做,那么包含就可以了
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
个集合。
如果您不想获得完整的对象,可以通过将AssignedAgents
作为匿名类型的属性合并来获得相同的效果:
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
};