2016-01-19 44 views
0
var PFOpeningList = (from emp in dbContext.EmployeeList 
           join dsg in dbContext.hrmDesig on emp.HrmDesignationId equals dsg.Id into DsgLeftJoin 
           from dsgleftjoin in DsgLeftJoin.DefaultIfEmpty() 
           join opb in dbContext.PfOpeningBalances on emp.Id equals opb.HrmEmployeeId into OpbLeftJoin 
           from opbleftjoin in OpbLeftJoin.DefaultIfEmpty() 
           where opbleftjoin.CmnCalendarYearId == clndrId 
             && (empId != 0 ? emp.Id == empId 
             : (dptId != 0 ? emp.HrmDepartmentId == dptId 
             : (officId != 0 ? emp.HrmOfficeId == officId : emp.CmnCompanyId == CmnId))) 
             && emp.CmnCompanyId == CmnId 
           select new 
           { 
            EmployeeId=emp.Id, 
            EmployeeName=emp.Name, 
            Designation = dsgleftjoin.Name, 
            OpeningIncome = (decimal?)opbleftjoin.OpeningIncome, 
            EmployeeContribution = (decimal?)opbleftjoin.EmployeeContribution, 
            CompanyContribution = (decimal?)opbleftjoin.CompanyContribution 
           }).ToList(); 

我希望通過EmployeeList實現所有員工的指定(hrmDesig)。使用日曆年進行過濾是強制性的。但是如果用戶選擇Office/Department/Employee,數據也應該被過濾。任何人都可以幫忙嗎?如何使用linq中的三元運算符過濾數據

+0

爲什麼不建立你的查詢更多的步驟,而不是一次嘗試? –

回答

0

你where條件是不對的,你應該是這樣的嘗試:

var PFOpeningList = (from emp in dbContext.EmployeeList 
    join dsg in dbContext.hrmDesig on emp.HrmDesignationId equals dsg.Id into DsgLeftJoin 
    from dsgleftjoin in DsgLeftJoin.DefaultIfEmpty() 
    join opb in dbContext.PfOpeningBalances on emp.Id equals opb.HrmEmployeeId into OpbLeftJoin 
    from opbleftjoin in OpbLeftJoin.DefaultIfEmpty() 
    where opbleftjoin.CmnCalendarYearId == clndrId 
     && (empId != 0 ? emp.Id == empId : true) 
     && (dptId != 0 ? emp.HrmDepartmentId == dptId : true) 
     && (officId != 0 ? emp.HrmOfficeId == officId : true) 
     && emp.CmnCompanyId == CmnId 
    select new 
    { 
     EmployeeId=emp.Id, 
     EmployeeName=emp.Name, 
     Designation = dsgleftjoin.Name, 
     OpeningIncome = (decimal?)opbleftjoin.OpeningIncome, 
     EmployeeContribution = (decimal?)opbleftjoin.EmployeeContribution, 
     CompanyContribution = (decimal?)opbleftjoin.CompanyContribution 
    }).ToList(); 

的想法是,如果你的濾波器參數爲0,則避免過濾條件設置爲true

1

有沒有需要在這裏所有使用條件運算符...我懷疑你想要的where條款:

where opbleftjoin.CmnCalendarYearId == clndrId 
    && (empId == 0 || emp.Id == empId) 
    && (dptId == 0 || emp.HrmDepartmentId == dptId) 
    && (officId != 0 ? emp.HrmOfficeId == officId) 
    && emp.CmnCompanyId == CmnId; 

牀$呃的是,你可以添加條件,步驟 - 只需是強制性入手:

var query = from emp in dbContext.EmployeeList 
      join dsg in dbContext.hrmDesig on emp.HrmDesignationId equals dsg.Id into DsgLeftJoin 
      from dsgleftjoin in DsgLeftJoin.DefaultIfEmpty() 
      join opb in dbContext.PfOpeningBalances on emp.Id equals opb.HrmEmployeeId into OpbLeftJoin 
      from opbleftjoin in OpbLeftJoin.DefaultIfEmpty() 
      where opbleftjoin.CmnCalendarYearId == clndrId 
       && emp.CmnCompanyId == CmnId 
      select new { emp, dsgleftjoin, opbleftjoin }; 

if (empId != 0) 
{ 
    query = query.Where(x => x.emp.Id == empId); 
} 
// etc 

另外請注意,您的查詢目前似乎假設dsgleftjoinopbleftjoin非空,什麼時候才能由於左連接很容易爲空。