2016-06-27 9 views
0

私は入力された各チェックリストのカウントを返す辞書結果があります。私は、私は以下のように返されるしたいの私の合計数を取得_context.Checklistsを返すときSQLに2番目のテーブルLinqを含む

public class COMPLAINT 
{ 
    [Key] 
    public int COMP_ID { get; set; } 
    public Nullable<DateTime> Received_DT { get; set; } 
    public virtual ICollection<CHECKLIST> CHECKLISTs { get; set; } 
} 

public class CHECKLIST 
{ 
    [Key] 
    public int CL_ID { get; set; } 
    public int EmpID { get; set; } 
    public int COMP_ID { get; set; } 
} 

クラスはこのように見えます。

public Dictionary<int, int> GetAllComplaintsCount() 
    { 
     try 
     { 
      return _context.Checklists 

       .GroupBy(a => a.EmpID) 
       .ToDictionary(g => g.Key, g => g.Count()); 
     } 
     catch (Exception ex) 
     { 
      _logger.LogError("Could not get am with checklist", ex); 
      return null; 
     } 
    } 

QUESTION しかし、私は苦情テーブルを追加するとき、私はEmpIDを苦情テーブルではありませんが、チェックリストテーブルにあるエラーが発生します。苦情処理テーブルとグループをEmpIDで含めるにはどうすればよいですか?

public Dictionary<int, int> GetAllComplaintsCount() 
    { 
     try 
     { 
      return _context.Complaints 
       .Include(t=> t.Checklists) 
       .GroupBy(a => a.EmpID) 
       .ToDictionary(g => g.Key, g => g.Count()); 
     } 
     catch (Exception ex) 
     { 
      _logger.LogError("Could not get am with checklist", ex); 
      return null; 
     } 
    } 
+0

あなたがチェックリストに苦情プロパティを含めるのを忘れましたか? –

+0

@RobertMcKeeごめんなさい、訂正しました。 – epv

+0

返されたいものは何ですか? –

答えて

1

はまだチェックリスト上の苦情プロパティが欠落し、それが他の監督だったと仮定すると、私は思うこれはあなたが望むものである:

public Dictionary<int, int> GetAllComplaintsCount(DateTime start, DateTime finish) 
{ 
    try 
    { 
    return _context.Checklists 
     .Where(a=>a.Complaint.Received_DT>=start && a.Complaint.Received_DT<finish) 
     .GroupBy(a => a.EmpID) 
     .ToDictionary(g => g.Key, g => g.Count()); 
    } 
    catch (Exception ex) 
    { 
    _logger.LogError("Could not get am with checklist", ex); 
    return null; 
    } 
} 
+0

チェックリストにクレームプロパティを追加する必要があることを認識していませんでした。追加したときに、あなたの提案が助けになりました。私はpublic virtual COMPLAINT COMPLAINT {get;を追加しました。セット; }をチェックリストのプロパティに追加して、受信した日付にアクセスするエラーを修正します。どうもありがとうございます。 – epv

関連する問題