c# - List loop operations with linq -
i've list this:
var query = enumerable.range(0, drea2.count / 4 * 1440).select((n, index) => { if ((index >= 480 && index < 750) || (index >= 810 && index < 1080)) return 0; else if (index >= 750 && index < 810) return 1; else return 2; });
but, range must changeable. example; i've list contains these indexes. these indexes may different each other.
1440 means 1440 min. of day. want add 1440 these indexes. example:
query[0], ... query[479] = 2 --- query [1440], ... query[1919] = 2 --- query[2880] = 2
query[480], .. query[749] = 0, --- query[810], .. query[1079] = 0, --- query[1920], .. query[2189] = 0..
so, whatever count of drea2 list, query has size (drea2.count / 4 * 1440)
how can this?
edit: if drea2.count() returns 6, if condition must have 6 different phrase per 1440 index. first if: (and query range must has 7200 size, now)
if ((index >= 480 && index < 750) || (index >= 810 && index < 1080)) return 0; // 1 if ((index >= 480 + 1440 && index < 750 + 1440) || (index >= 810 + 1440 && index < 1080 + 1440)) return 0; // 2 ... // 3 (+ 2880) ... // 4 (+ 4320) ... // 5 (+ 5760) if ((index >= 480 + 7200 && index < 750 + 7200) || (index >= 810 + 7200 && index < 1080 + 7200))
as per updates op, have rewritten answer. reference of previous version, follow this link.
here proposed solution, assuming drea2
collection array, or list (thus implementing ilist<int>
interface) , each 4
items in list form ranges used in produced sequences of 1440
items:
public ienumerable<int> getquery(ilist<int> drea2) { var count = drea2.count; var result = new int[(count/4)*1440]; (var = 0, fillindexstart = 0; < count; i+= 4, fillindexstart += 1440) { var rangeindices = new[] { drea2[i], drea2[i+1], drea2[i+2], drea2[i+3] }; (var j = 0; j < rangeindices[0]; j++) { result[fillindexstart + j] = 2; } (var j = rangeindices[3]; j < 1440; j++) { result[fillindexstart + j] = 1; } } return result; }
unfortunately, there no linq
here, hope needed.
the previous code have used condition, instead of 2 loops in method above:
if ((j >= rangeindices[0] && j < rangeindices[1]) || (j >= rangeindices[2] && j < rangeindices[3])) result[fillindexstart + j] = 0; else if (j >= rangeindices[1] && j < rangeindices[2]) result[fillindexstart + j] = 1; else result[fillindexstart + j] = 2;
since array filled default values, can take advantage of result
being full of zeros. why have rewritten conditions 2 for
loops
note. code above relies on drea2.count / 4
0
. otherwise, indexoutofrangeexception
when initializing rangeindices
array.
Comments
Post a Comment