2016-12-28 6 views
0

私は、Entity Frameworkと私のモデルを使用しています、私は3つのオプションのフィルタがあるレポート持たこのEntity FrameworkのLINQのは、JOINとWHERE

enter image description here

のような関係があります:日から従業員番号、日付を

通常のSQLで、私はこの

public List<Employee> SearchAttendance(string fromDate, string toDate, string employeeNo) 
{ 
    string query = "SELECT e.first_name, e.last_name, i.employeeNo, t.timeIn, t.timeOut, t.imagePath 
    FROM Employees AS e LEFT JOIN EmploymentInfo AS i ON e.id = i.employeeId 
    LEFT JOIN Trackers AS t ON i.id = t.employeeId "; 
    if (fromDate != "") 
    { 
     if (toDate == "") 
     { 
      query += "where t.timeIn = '" + Datetime.Parse(fromDate) + "' "; 
     } 
     else 
     { 
      query += "where t.timeIn >= '" + Datetime.Parse(fromDate) + "' "; 
      query += "and t.timeIn <= '" + DateTime.Parse(toDate) + "' "; 
     } 
     if (employeeNo != "") 
     { 
      query += "and "; 
     } 
    } 

    if (employeeNo != "") 
    { 
     if (fromDate == "") 
     { 
      query += "where "; 
     } 
     query += "i.employeeNo = '" + employeeNo + "' "; 
    } 
    query += "ORDER BY t.timeIn DESC"; 
    var res = _context.Database.SqlQuery<Employee>(query).ToList(); 
    return res; 
} 

をしたいと思います。しかし、私はEntity Frameworkのを使用していますので、このコードは正しく表に入社されません。私は関連するテーブルとその結果を持つ従業員モデルを取得する必要があります

これまでのところ、フィルタなしで全データを表示しました。 EFで秘密

public List<Employee> SearchAttendance(string fromDate, string toDate, string employeeNo) 
    { 
     var employee = _context.Employees 
           .Include(i => i.EmploymentInfoes) 
           .Include(i => i.EmploymentInfoes.Select(c => c.Trackers)); 

     if (!string.IsNullOrEmpty(fromDate)) 
     { 
      employee = employee.Where(xx => xx.timeIn <= DateTime.Parse(fromDate)); 
     } 

     if (!string.IsNullOrEmpty(toDate)) 
     { 
      employee = employee.Where(xx => xx.timeIn >= DateTime.Parse(toDate)); 
     } 

     if (!string.IsNullOrEmpty(employeeNo)) 
     { 
      employee = employee.Where(xx => xx.employeeNo == employeeNo); 
     } 

     return employee.ToList(); 
    } 

:私は必要なのは、あなたが

public List<Employees> SearchAttendance(string fromDate, string toDate, string employeeNo) 
{ 
     var employees = _context.Employees 
         .Include(i => i.EmploymentInfoes) 
         .Include(i => i.EmploymentInfoes.Select(c => c.Trackers)) 
         .Select(i=> new { // your properties you need }) 
         .AsQueryable(); 

     if (fromDate != "") 
     { 
      employees = employees.where(t => t.timeIn >= DateTime.Parse(fromDate)); 
     } 

     if (toDate != "") 
     { 
      employees = employees.where(t => t.timeIn <= DateTime.Parse(toDate)); 
     } 

     if (employeeNo != "") 
     { 
      employees = employees.where(t => t.employeeNo == employeeNo); 
     } 


     return employees.ToList(); 
} 
+0

以下の回答を確認して、テストしてみてください。 –

答えて

1

F Quarableデータを返します....このような何かをしようとどのように私は、必要に応じて

public List<Employee> SearchAttendance(string fromDate, string toDate, string employeeNo) 
{ 
    var employee = _context.Employees 
        .Include(i => i.EmploymentInfoes) 
        .Include(i => i.EmploymentInfoes.Select(c => c.Trackers)) 
        .ToList(); 

    return employee; 
} 
+0

問題は私がxx => xxを使うときです。私は従業員モデル – natsu1627

+0

はいのプロパティにアクセスすることができます...しかし、あなたのようなサブエンティティにもアクセスする必要がある場合トラッカー..このような何かをしようとすると(!string.IsNullOrEmpty(fromDate))employee = employee.Where xx => xx.EmploymentInfoes.Select(c => c.Trackers).Any(tt => tt。timeIn <= DateTime.Parse(fromDate)) –

1

それをフィルタリングすることができていますあなたのエンティティをQueriableのままにしておくことです(ToList()をしないように)

+0

問題は私がt => tを使うときです。私は従業員モデル – natsu1627

+0

のプロパティにアクセスすることができます。必要な分量を選択してください.Select(i => new {//必要なプロパティ}) ' –

+0

@ natsu1627あなたの問題を解決しましたか? –

関連する問題