我尽力学习LINQ,但我仍然很难编写它。像这样,假设我有一个数据集或列表可能,集合对象的名称或字段是列名。
Id |日期|周一|周二|周三|周四 |周五|周六|周日|计数
1 | 01/05 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 0 = | = 0 == | == 5< - (1)
2 | 02/02 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 0 = | = 0 == | == 5 ** | - (2)
3 | 03/02 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 0 = | = 0 == | == 5 ** | - (2)
4 | 04/06 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 1 = | = 1 == | == 7< - (1)
5 | 05/04 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 1 = | = 1 == | == 7 ** | - (3)
6 | 06/01 | = 1 = | == 1 == | == 1 = | == 1 = | 1 = | = 1 = | = 1 == | == 7 ** | - (3)
7 | 07/06 | = 1 = | == 1 == | == 1 = | == 1 = | 0 = | = 0 = | = 0 == | == 4< ----(1)
8 | 08/03 | = 1 = | == 1 == | == 1 = | == 1 = | 0 = | = 0 = | = 0 == | == 4 ** | - (4)
9 | 09/07 | = 1 = | == 1 == | == 1 = | == 1 = | 0 = | = 0 = | = 0 == | == 4 ** | - (4)
10 | 10/05 | 1 = | == 1 == | == 1 = | == 1 = | 0 = | = 0 = | = 0 == | == 4 ** | - (4)
我想要的只是首先获得所有数字(1)然后得到(2)因为它们属于第一个(1)。接下来是(3)的组,因为它们属于第二个(1)。最后是(4)的组,因为它们属于最后一个(1)。
请帮忙。
- 问题改写。 1.我怎样才能获得第一组5分,然后是7分组,最后是4分组?
答案 0 :(得分:5)
您似乎想要按计数订购。
当你说“得到第一组5个”时你的意思是什么 - 你想得到什么数据?
澄清之后更新
假设
public class Row
{
public int ID{get;set;}
public string Date{get;set;}
public int Count{get;set;}
}
Row r1 = new Row{ID=1, Date="01/01/01", Count=5};
Row r2 = new Row{ID=2, Date="01/02/01", Count=5};
Row r3 = new Row{ID=3, Date="01/03/01", Count=5};
Row r4 = new Row{ID=4, Date="01/04/01", Count=7};
Row r5 = new Row{ID=5, Date="01/05/01", Count=7};
Row r6 = new Row{ID=6, Date="01/06/01", Count=7};
Row r7 = new Row{ID=7, Date="01/07/01", Count=4};
Row r8 = new Row{ID=8, Date="01/08/01", Count=4};
Row r9 = new Row{ID=9, Date="01/09/01", Count=4};
Row r10 = new Row{ID=10, Date="01/01/01", Count=4};
List<Row> rows = new List<Row>{r1,r2,r3,r4,r5,r6,r7,r8,r9,r10};
然后
// We will assign results of our query to this variable
var result =
// rows is a generic list of Row objects
rows
// This splits the list into seperate categories organised by Count
// After the GroupBy, we have an IEnumerable<IGrouping<Int32, Row>> - that is, a collection of collections of items sharing a common key (in this case Count)
.GroupBy(r=>r.Count) // r is of type Row
// Now we are simply selecting the first item of each subgroup.
.Select(g=>g.First()) // g is IGrouping<Int32,Row>, g.First() is of type Row
;
给出
ID Date Count
1 01/01/01 5
4 01/04/01 7
7 01/07/01 4