c# - Linq group by, order by values -
i need have table:
id day group value ------------------------------------------------- 1 2016-10-11 1 10.5 2 2016-10-11 1 10.8 3 2016-10-11 1 10.7 4 2016-10-11 1 10.6 5 2016-10-11 1 10.5 6 2016-10-11 1 10.4 7 2016-10-11 1 10.8 8 2016-10-11 1 10.2 9 2016-10-11 1 10.0 10 2016-10-11 1 10.9 11 2016-10-11 2 10.1 12 2016-10-11 2 10.0 13 2016-10-11 2 10.1 14 2016-10-11 2 10.6 15 2016-10-11 2 10.7 16 2016-10-11 2 10.2 17 2016-10-11 2 10.0 18 2016-10-11 2 10.5 19 2016-10-11 2 10.5 20 2016-10-11 2 10.8 21 2016-10-12 1 11.1 22 2016-10-12 1 11.7 23 2016-10-12 1 11.0 24 2016-10-12 1 11.4 25 2016-10-12 1 11.7 26 2016-10-12 1 11.8 27 2016-10-12 1 11.1 28 2016-10-12 1 11.1 29 2016-10-12 1 11.4 30 2016-10-12 1 11.6 31 2016-10-12 2 11.9 32 2016-10-12 2 11.6 ...
and want
- order value
- group group
- group day
- and value like:
[[[10.5,10.8],[10.0,10.1]],[[11.1,11.7],[11.6,11.9]]]
(example)
respectively need work value more, when value example more value in day. , split batch 4 batch , choose first value every group. have 4 value every group , 1 day have 2 batch , every batch have 4 value. , because need have 5 value last value in every batch find max value , add batch 4 value. don’t know if understand there code:
var valuelist = await task.factory.startnew(() => ( pv in db.values dbfunctions.truncatetime(pv.day) >= startdate && dbfunctions.truncatetime(pv.day) <= enddate orderby pv.value //group pv pv.day gpv select pv )); var group1 = await task.factory.startnew(() => ( ( vl in valuelist vl.group == 1 select vl) )); var group2 = await task.factory.startnew(() => ( vl in valuelist vl.group == 2 select vl )); var group3 = await task.factory.startnew(() => ( vl in valuelist vl.group == 3 select vl )); var group1split = group1.split(group1.count() / 4 + 1).select(x => x.first()); var group1max = group1.max(); var group2split = group2.split(group2.count() / 4 + 1).select(x => x.first()); var group2max = group2.max(); var group3split = group3.split(group3.count() / 4 + 1).select(x => x.first()); var group3max = group3.max(); var group1values = group1split.concat(group1max); var group2values = group2split.concat(group2max); var group3values = group3split.concat(group3max); var groupallvalues = group1values.concat(group2values).concat(group3values); var groupsplitallvalues = groupallvalues.split(5); return request.createresponse(httpstatuscode.ok, groupsplitallvalues.tolist());
update:
this work:
var result = db.values .where(x => (dbfunctions.truncatetime(x.day)) >= startdate && (dbfunctions.truncatetime(x.day)) <= enddate) .groupby(x => new { day = x.day, group = x.group }) .select(x => x.select(y => y.value).orderby(z => z).tolist()).tolist();
i this:
[ [10.0, 10.2, 10.4, 10.5, 10.5, 10.6, 10.7, 10.8, 10.8, 10.9], [10.0, 10.0, 10.1, 10.1, 10.2, 10.5, 10.5, 10.6, 10.7, 10.8], [11.0, 11.1, 11.1, 11.1, 11.4, 11.4, 11.6, 11.7, 11.7, 11.8], [...] ]
but need split every batch in case 2 part , select first value part. have 2 value , want concat 2 value max value batch example final result be:
[ [10.0, 10.6, 10.9], [10.0, 10.2, 10.8], [11.0, 11.4, 11.8], [...] ]
for splitting use method:
public static ienumerable<ienumerable<t>> split<t>(this ienumerable<t> source, int length) { if (length <= 0) throw new argumentoutofrangeexception("length"); var section = new list<t>(length); foreach (var item in source) { section.add(item); if (section.count == length) { yield return section.asreadonly(); section = new list<t>(length); } } if (section.count > 0) yield return section.asreadonly(); }
try this
using system; using system.collections.generic; using system.linq; using system.text; using system.data; namespace consoleapplication23 { class program { static void main(string[] args) { datatable dt = new datatable(); dt.columns.add("id", typeof(int)); dt.columns.add("day", typeof(datetime)); dt.columns.add("group", typeof(int)); dt.columns.add("value", typeof(decimal)); dt.rows.add(new object[] {1 , datetime.parse("2016-10-11"), 1, 10.5}); dt.rows.add(new object[] {2 , datetime.parse("2016-10-11"), 1, 10.8}); dt.rows.add(new object[] {3 , datetime.parse("2016-10-11"), 1, 10.7}); dt.rows.add(new object[] {4 , datetime.parse("2016-10-11"), 1, 10.6}); dt.rows.add(new object[] {5 , datetime.parse("2016-10-11"), 1, 10.5}); dt.rows.add(new object[] {6 , datetime.parse("2016-10-11"), 1, 10.4}); dt.rows.add(new object[] {7 , datetime.parse("2016-10-11"), 1, 10.8}); dt.rows.add(new object[] {8 , datetime.parse("2016-10-11"), 1, 10.2}); dt.rows.add(new object[] {9 , datetime.parse("2016-10-11"), 1, 10.0}); dt.rows.add(new object[] {10, datetime.parse("2016-10-11"), 1, 10.9}); dt.rows.add(new object[] {11, datetime.parse("2016-10-11"), 2, 10.1}); dt.rows.add(new object[] {12, datetime.parse("2016-10-11"), 2, 10.0}); dt.rows.add(new object[] {13, datetime.parse("2016-10-11"), 2, 10.1}); dt.rows.add(new object[] {14, datetime.parse("2016-10-11"), 2, 10.6}); dt.rows.add(new object[] {15, datetime.parse("2016-10-11"), 2, 10.7}); dt.rows.add(new object[] {16, datetime.parse("2016-10-11"), 2, 10.2}); dt.rows.add(new object[] {17, datetime.parse("2016-10-11"), 2, 10.0}); dt.rows.add(new object[] {18, datetime.parse("2016-10-11"), 2, 10.5}); dt.rows.add(new object[] {19, datetime.parse("2016-10-11"), 2, 10.5}); dt.rows.add(new object[] {20, datetime.parse("2016-10-11"), 2, 10.8}); dt.rows.add(new object[] {21, datetime.parse("2016-10-12"), 1, 11.1}); dt.rows.add(new object[] {22, datetime.parse("2016-10-12"), 1, 11.7}); dt.rows.add(new object[] {23, datetime.parse("2016-10-12"), 1, 11.0}); dt.rows.add(new object[] {24, datetime.parse("2016-10-12"), 1, 11.4}); dt.rows.add(new object[] {25, datetime.parse("2016-10-12"), 1, 11.7}); dt.rows.add(new object[] {26, datetime.parse("2016-10-12"), 1, 11.8}); dt.rows.add(new object[] {27, datetime.parse("2016-10-12"), 1, 11.1}); dt.rows.add(new object[] {28, datetime.parse("2016-10-12"), 1, 11.1}); dt.rows.add(new object[] {29, datetime.parse("2016-10-12"), 1, 11.4}); dt.rows.add(new object[] {30, datetime.parse("2016-10-12"), 1, 11.6}); dt.rows.add(new object[] {31, datetime.parse("2016-10-12"), 2, 11.9}); dt.rows.add(new object[] {32, datetime.parse("2016-10-12"), 2, 11.6}); var results = dt.asenumerable() .groupby(x => new { day = x.field<datetime>("day"), group = x.field<int>("group")}) .select(x => x.select(y => y.field<decimal>("value")).orderby(z => z).tolist()).tolist(); list<list<decimal>> groups = new list<list<decimal>>(); const int num_groups = 2; foreach (var result in results) { int size = result.count; int groupsize = (int)(size / num_groups); //round down list<decimal> newgroup = new list<decimal>() { result[0] }; groups.add(newgroup); (int groupnum = 0; groupnum < num_groups; groupnum++) { newgroup.add( result.skip(groupnum * groupsize).take(groupsize).max()); } } } } }
Comments
Post a Comment