Search code examples
asp.net-mvcentity-frameworklinqlinq-to-entities

LINQ - Multi Join Tables MVC C#


LINQ - Multi Join Tables MVC C#

Hey, plz i need to help, where is the error in my codes ad what is the solution

Model :

public class LeaveInitialBalanceViewModelKendoGrid
    {
        [HiddenKendoGridColumn]
        public string ID { get; set; }
        public string EmployeeCode { get; set; }
        public string EmployeeName { get; set; }
        public string Department { get; set; }
        public string Section { get; set; }
        public string LeaveType { get; set; }

        public string LeaveBalance { get; set; }
        public string PreviousBalance { get; set; }

        public string LastModifyDate { get; set; }
        public string LastModifyUserID { get; set; }

    }

Linq Query :

 public IQueryable<LeaveInitialBalanceViewModel> GetEmployeeCurrentBalance()
        {
            IQueryable<LeaveInitialBalanceViewModelKendoGrid> lst =
                (from LvBalance in _context.EmployeeLeaveBalances
                 join LvConf in _contextSetup.LeaveVacationsConfigs 
                 on LvBalance.LeaveTypeId equals LvConf.Id
                 join LvSubType in _contextSetup.LeaveVacationsSubTypes 
                 on LvConf.Id equals LvSubType.ParentTypeId

                 join Emp in _context.Employees on LvBalance.EmployeeId equals Emp.Id
                 join Dept in _contextSetup.Departments on Emp.DepartmentId equals Dept.Id
                  .Select( x => new LeaveInitialBalanceViewModelKendoGrid
                  {
                      ID = x.,
                      EmployeeCode =,
                      EmployeeName =,
                      Department =,
                      Section =,
                      LeaveType =,
                      LeaveBalance =,
                      PreviousBalance =,
                      LastModifyDate =,
                      LastModifyUserID =

                  });

            return lst;
        }

--- and How i can full outer join not inner join

join Sec in _contextSetup.Sections on emp.secID equals Sec.ID

please help me , my problem is : te query in linq not working i need : to solve this problem UW, tx you so much (;

{"Object reference not set to an instance of an object."}


Solution

  • Seems to me like you are mixing the Linq query syntax and method syntax:

    List<long> numbers = new List<long>(){1,2,3,4,5};
    
    //Query syntax:
    var querySyntax = (from a in numbers
             where a > 3
             select a);
    
    //MethodSyntax       
    var methodSyntax = numbers.Where(x => x > 3);
    
    //both returns 4,5
    

    More information here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq

    In your case it should be something like:

    IQueryable<LeaveInitialBalanceViewModelKendoGrid> lst =
                    (from LvBalance in _context.EmployeeLeaveBalances
                     join LvConf in _contextSetup.LeaveVacationsConfigs
                     on LvBalance.LeaveTypeId equals LvConf.Id
                     join LvSubType in _contextSetup.LeaveVacationsSubTypes
                     on LvConf.Id equals LvSubType.ParentTypeId
    
                     join Emp in _context.Employees on LvBalance.EmployeeId equals Emp.Id
                     join Dept in _contextSetup.Departments on Emp.DepartmentId equals Dept.Id
                     select new LeaveInitialBalanceViewModelKendoGrid()
                     {
                        ID = LvBalance.Id,
                         EmployeeCode = Emp.Code,
                         EmployeeName = Emp.Name,
                         [ and so on]
                     });