使用下表:
+-------------+-----------+---------+
| Parent_ID | Item_ID | Count |
+-------------+-----------+---------+
| 1 | 1 | 1 |
| 1 | 1 | 5 |
| 1 | 1 | 4 |
| 1 | 2 | 7 |
| 1 | 2 | 2 |
| 2 | 1 | 2 |
| 2 | 1 | 3 |
| 2 | 2 | 2 |
| 2 | 2 | 4 |
+-------------+-----------+---------+
我想获得给定Parent_ID
和的最高可用计数Item_ID
,如下所示:
+-------------+-----------+---------+
| Parent_ID | Item_ID | Count |
+-------------+-----------+---------+
| 1 | 1 | 5 |
| 1 | 2 | 7 |
| 2 | 1 | 3 |
| 2 | 2 | 4 |
+-------------+-----------+---------+
我将如何在 C# 中使用 LINQ/SQL 执行此操作?例如,我想要父 id 1 的最高项目数。这将是这样的:
int parentId = 1;
var counts = from c in database.Items where parentId == c.parentId
//this gets all counts from items for parent id 1, but I am just looking
//for the highest count per item for parent id 1