Linq 2表联接但需要来自3的数据不需要联接所有3

时间:2019-04-15 02:37:02

标签: c# entity-framework linq asp.net-mvc-5

我有3个表,我实际上需要从2个表中获取数据,我需要使用Linq进行联接,但是我需要投影的项目名称,并且需要插入将其转换为使用linq创建的同一模型。我在context.projects中尝试了proj.ProjectId = detail.ProjectId,但是它说不能将int转换为bool,并且我在任何地方都没有声明bool?这是我的linq。而整个方法实际上

PS-这是我创建的最新动作,但仍然不是100%正确,但这是否有获取3个表数据的正确想法?

     public ActionResult DisplayAllTimeSheetDetails(int masterid)
        {
            var masterModel = context.TimeSheetMaster.Where(w => 
                              w.TimeSheetMasterId.Equals(masterid)).FirstOrDefault();

            var detailM = context.TimeSheetDetails.Where(t => 
                          t.TimeSheetMasterId.Equals(masterModel.TimeSheetMasterId))
                          .FirstOrDefault();

            var project = context.Projects.Where(p => p.ProjectId.Equals(
                          detailM.ProjectId)).FirstOrDefault();


            var details = (from master in context.TimeSheetMaster
                           join detail in context.TimeSheetDetails
                           on master.TimeSheetMasterId equals detail.TimeSheetMasterId
                           //from proj in context.Projects where proj.ProjectId = detail.ProjectId
                           select new TimeSheetDetailsModel()
                           {
                               Sunday = detail.Sunday,
                               Monday = detail.Monday,
                               Tuesday = detail.Tuesday,
                               Wednesday = detail.Wednesday,
                               Thursday = detail.Thursday,
                               Friday = detail.Friday,
                               Saturday = detail.Saturday,
                               Hours = detail.Hours,
                               Comment = master.Comment,
                               ProjectName = project.ProjectName
                           }).ToList();

            return View(details);
        }
public class TimeSheetDetailsModel
    {
        [Display(Name = "Time Sheet ID")]
        public int TimeSheetId { get; set; }
        [Display(Name = "Day of Week")]
        public string DaysOfWeek { get; set; }
        [Display(Name = "Hours")]
        public int? Hours { get; set; }
        [Display(Name = "Period")]
        public string Period { get; set; }
        [Display(Name = "Project ID")]
        public int ProjectId { get; set; }
        [Display(Name = "User ID")]
        public string UserId { get; set; }
        [Display(Name = "Date Created")]
        public DateTime DateCreated { get; set; }
        [Display(Name = "Time Sheet Master ID")]
        public int TimeSheetMasterId { get; set; }
        [Display(Name = "Project Name")]
        public string ProjectName { get; set; }
        public string Comment { get; set; }
        public int Sunday { get; set; }
        public int Monday { get; set; }
        public int Tuesday { get; set; }
        public int Wednesday { get; set; }
        public int Thursday { get; set; }
        public int Friday { get; set; }
        public int Saturday { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

我让它像这样工作。我愿意接受其他答案和更干净的方法。

public ActionResult DisplayAllTimeSheetDetails(int masterid)
            {
            var masterModel = context.TimeSheetMaster.Where(w => 
                              w.TimeSheetMasterId.Equals(masterid)).FirstOrDefault();

            var detailM = context.TimeSheetDetails.Where(t =>
                          t.TimeSheetMasterId.Equals(masterModel.TimeSheetMasterId)).FirstOrDefault();//.ToList();

            var project = context.Projects.Where(p => p.ProjectId==detailM.ProjectId).FirstOrDefault();


            var details = (from master in context.TimeSheetMaster
                           join detail in context.TimeSheetDetails
                           on master.TimeSheetMasterId equals detail.TimeSheetMasterId
                           select new TimeSheetDetailsModel()
                           {
                               Sunday = detail.Sunday,
                               Monday = detail.Monday,
                               Tuesday = detail.Tuesday,
                               Wednesday = detail.Wednesday,
                               Thursday = detail.Thursday,
                               Friday = detail.Friday,
                               Saturday = detail.Saturday,
                               Hours = detail.Hours,
                               Comment = master.Comment,
                               ProjectName = project.ProjectName
                           }).FirstOrDefault();

            return View(details);
        }