Joining three entities using linq

Gcobani54

Member
Joined
Apr 26, 2021
Messages
7
Programming Experience
3-5
Hi Team

I need some help in joining three entities using linq, i have one join but the problem i have i am getting a repetion of years, weekNum and day colums with data being incorrect.
Using Linq to join 3 entities:
  public IList<ExtractionViewModel> GetExtractionViewModels(string year, int week)
        {

            ProductionManagementEntities db = new ProductionManagementEntities();

            
            var scheduleList = (from p in db.ProductionDays
                                join w in db.Weeks
                                // join m in db.Models somewhere here to prevent duplicate records
                                on p.WeekId equals w.WeekId

                                orderby w.Year ascending
                                orderby w.WeekNum ascending


                                where (w.WeekNum == 5)

                                select new ExtractionViewModel
                                {

                                    Day = p.ProductionDate,
                                    Week = w.WeekNum,

                                    // Model = m.Name,
                                    Year = w.Year

                                }).ToList();
            return scheduleList;
        }

 public class ExtractionViewModel
    {
        public string Year { get; set; }

        public int Week { get; set; }

        
        [DataType(DataType.Date)]
        public DateTime Day { get; set; }

       // public string VW240 { get; set; }

        public string VW250 { get; set; }

        public string VW270 { get; set; }

        public string VW2502PA { get; set; }

        public string VW270PA { get; set; }
    }
 
I see one entity. Where are the other two entities? How are you doing the JOIN?

I think this is related to your other thread. You are not looking for a JOIN operation. You are looking for a grouping operation.
 
Hi Team

I have a method that calls three 3 tables, but now i am getting an error says ' variable does not exist in current context'. How do i fix this issue?
3 tables in linq using c#:
       public IList<ExtractionViewModel> GetExtractionViewModels(string year, int week)
        {

            ProductionManagementEntities db = new ProductionManagementEntities();


            var scheduleList = (from p in db.Weeks
                                from pr in db.ProductionDays on p.WeekId = p.WeekId // Error is here does not see this in current context

                                orderby w.Year ascending
                                orderby w.WeekNum ascending


                                where (w.WeekNum == 5)

                                select new ExtractionViewModel
                                {

                                    Day = p.ProductionDate,
                                    Week = w.WeekNum,

                                    // Model = m.Name,
                                    Year = w.Year

                                }).ToList();
            return scheduleList;
        }
 
Please stop opening new threads. Instead reply back on your previous thread. By replying back on your previous thread, we can see the evolution of your code and have half a chance at trying to discern which direction you are headed. (Only open a new thread if you are trying to solve something that is completely new.)

As an example, with this thread, it's not obvious why you are trying to do a join. It's only if users read your other thread about trying to build a "dynamic" table, will it become clear as to what you are trying to achieve.

Merging threads...
 
Last edited:

Latest posts

Back
Top Bottom