0

在我的应用程序中,我想显示给定用户的历史详细信息。一个用户可以有许多工作,并且可以访问具有不同角色的不同促销活动。

我希望能够展示这种历史

               晋升

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
4

1 回答 1

0

你试试这个:

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 
         let year=job.JobStartDate.HasValue ? job.JobStartDate.Value.Year : promo.PromotionStartDate.HasValue ? promo.PromotionStartDate.Value.Year : 0
             select new
             {
                 Year =year ,
                 JobTitle = job.JobTitle,
                 Promotion = "Promotion "+year
             }).Distinct().ToList();
于 2013-07-30T09:37:10.487 回答