使用条件查找不同的列 ID 和列数

Find distinct column id and column count with condition

我有一个 table bookorder_t 列如下 orderid,bookid,ordersuccess。我想找到 ordersuccess= 1 的所有 bookids, 和书籍 ID 的数量。

假设这是我的 bookorder_t

orderid bookid  ordersuccess
----------------------------
100      1         1
101      1         null
102      1         1
103      2         1
104      2         1
106      1         1

我的预期结果是

bookid count
1       3
2       2

如何在 linq 中为此编写查询?

这应该会给你正确的结果:-

var result = db.bookorder_t.Where(x => x.ordersuccess == 1)
                           .GroupBy(x => x.bookid)
                           .Select(x => new 
                                      {
                                           bookid  = x.Key,
                                           count = x.Count()
                                      });