LINQ TO SQL存储库模式组和计数

时间:2016-06-02 18:21:42

标签: sql linq linq-to-sql

我有一个linq to sql查询,我从中提取数据很好,但不确定如何添加组或计数。

Linq代码

public IEnumerable<COMPLAINT> GetAllCount()
    {
        try
        {
            return from m in _context.Comp
                   join p in _context.Checklists on m.ID equals p.C_ID
                   orderby m.Received_DT descending
                   select m;

        }
        catch (Exception ex)
        {
            _logger.LogError("Could not get complaint", ex);
            return null;
        }
    }

结果

[
 {
"comP_ID": 909,
"received_DT": "2018-01-22T00:00:00",
"Outcome": "Unsatisfactory",
"RecFinding": "Decline"
},
 {
"comP_ID": 909,
"received_DT": "2018-01-22T00:00:00",
"Outcome": "Unsatisfactory",
 "RecFinding": "Sustained"
},
 {
"comP_ID": 909,
"received_DT": "2018-01-22T00:00:00",
"Outcome": "satisfactory",
 "RecFinding": "Decline"
},
 {
"comP_ID": 909,
"received_DT": "2018-01-22T00:00:00",
"Outcome": "satisfactory",
"RecFinding": "Sustained"
},
]

问题

我对LINQ To SQL相当新,并且还编写了Fluent API来返回数据,但我不确定如何只返回按结果分组的一个变量,并计算如下:

[
 {
"Decline": 2,
"Sustained": 2,
},
]

或者

[
 {
"Unsatisfactory": 2,
"satisfactory": 2,
},
]

我已经尝试了几个组然后选择新方法,但我真的很困惑如何将它合并到linq代码中。谢谢。

0 个答案:

没有答案