Get Data from Multiple Tables using Linq query in ASP.Net MVC

Last Reply on Apr 20, 2019 08:15 PM By bigbear

Posted on Apr 15, 2019 12:15 AM

Hello,

I have 3 tables I need to get data from well actually only 2 that I need to do a join with which I know using Linq but I need the name of a project from the projected and I need to insert it into the same model Im creating using linq. I tried from proj in context.Projects where proj.ProjectId = detail.ProjectId but it says cant convert int to bool and I don’t have bool declared anywhere? Here is my linq. And the whole method actually

        public ActionResult DisplayAllTimeSheetDetails(TimeSheetMasterModel masterModel, TimeSheetDetailsModel detailsModel)
        {
            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,
                           }).ToList();
            //var project = context.Projects.Where(p => p.ProjectId.Equals(
                          //detailsModel.ProjectId));

 

You are viewing reply posted by: bigbear on Apr 20, 2019 08:15 PM.
Posted on Apr 20, 2019 08:15 PM

This is how I solved it.

            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==detailM.ProjectId)
                          .FirstOrDefault();


            var details = (from master in context.TimeSheetMaster
                           join detail in context.TimeSheetDetails
                           on master.TimeSheetMasterId equals detail.TimeSheetMasterId
                           where master.TimeSheetMasterId == masterid
                           //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
                           }).FirstOrDefault();