【发布时间】:2013-07-30 09:13:27
【问题描述】:
在我的应用程序中,我想显示给定用户的历史详细信息。一个用户可以有很多工作,并且可以使用不同的角色访问不同的促销活动。
我希望能够展示这种历史
年份 职位 促销
2012 助理 2012年促销
2013 经理 2013年促销
我试过这个查询(我正在使用 Linq 到实体)
var query = (from u in context.Users
join j in context.Jobs.Where(jo => jo.JobStartDate.HasValue) on u.UserID equals j.UserID into jobs
from job in jobs.DefaultIfEmpty()
join uir in context.UserInRoles on u.UserID equals uir.UserID into userInRoles
from userInRole in userInRoles.DefaultIfEmpty()
join rip in context.RoleInPromotion on userInRole.RoleInPromotionID equals rip.RoleInPromotionID into roleInPromotions
from roleInPromotion in roleInPromotions.DefaultIfEmpty()
join p in context.Promotions.Where(pro => pro.PromotionStartDate.HasValue) on roleInPromotion.PromotionID equals p.PopulationTargetID into promotions
from promo in promotions.DefaultIfEmpty()
where u.UserID == userId
select new
{
Year = job.JobStartDate.HasValue ? job.JobStartDate.Value.Year : promo.PromotionStartDate.HasValue ? promo.PromotionStartDate.Value.Year : 0,
JobTitle = job.JobTitle,
Promotion = promo.PromotionName
}).Distinct().ToList();
但这给了我这个结果,即使用户在 2012 年无法访问任何促销活动
年份 职位 促销
2012 助理 2013年促销
2013 经理 2013年促销
我也试过了,但似乎没有任何改变
group new { job, promo } by new { jobYear = job.JobStartDate.Value.Year, promoYear = promo.PromotionStartDate.Value.Year } into grp
【问题讨论】:
-
我会在 SQL 中设计这样一个查询,并可能创建一个视图或存储过程并调用它。
标签: sql database linq linq-to-entities