-1

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."}

  • can you share error stack? – Hitesh Kansagara Apr 21 '18 at 15:15
  • You really need to post more information for someone to help. What is the error you are receiving / behaviour? Also it's probably wise to post all your model classes ... – KnowHoper Apr 21 '18 at 15:16
  • 1
    What is the exact nature of the problem? You should be more specific... That said - I think you are mixing the Linq query syntax and method syntax. Look here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq When using the query syntax, the select at the end should not have a dot prefix (and it should be lowercase). Try writing you select like: select new LeaveInitialBalanceViewModelKendoGrid(){ ID = LvBalance.Id, EmployeeCode = Emp.EmployeeCode, [...] } and so on – user2384366 Apr 21 '18 at 15:19
  • plz i need to answer , i'm adding photo – Søśø ÐålợȜà ZåȜră Apr 21 '18 at 15:21
  • @user2384366 , plz see my editing plz error now is : {"Object reference not set to an instance of an object."} – Søśø ÐålợȜà ZåȜră Apr 21 '18 at 16:25
  • @SøśøÐålợȜàZåȜră Obviously you have a null somewhere. If any of the properties you are using ToString() on are null, then you'll get that exception. If unsure, the proper way is to check each nullable property like: ID = (LvBalance.Id != null ? LvBalance.Id.ToString() : ""). I'm not sure of this is the best way - but it works. Also, if you are using Entity Framework - keep in mind that the ToString() was added in the v6.1 or so - earlier you would get an Exception that it was not implemented. – user2384366 Apr 21 '18 at 16:35

1 Answers1

1

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://docs.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]
                 });
user2384366
  • 964
  • 2
  • 10
  • 25