Monday, 15 April 2013

c# - Get data in time range pattern when data and time are in differents arrays -



c# - Get data in time range pattern when data and time are in differents arrays -

i have array of info (double[] data) , list of datetimes (list datetimes). each position of info array related position of datetimes. mean: data[i] collected in datetimes[i].

now want filter info collected week pattern (7 day, 24 hours). so, have week pattern:

class weekpattern { list<daypattern> week; public weekpattern(list<daypattern> _week) { week = _week; } public bool isinrange(datetime time) { homecoming week.any(i => i.isinrange(time)); } } class daypattern { dayofweek day; list<bool> hours; public daypattern(list<bool> _hours, dayofweek _day) { hours = _hours; day = _day; } public bool isinrange(datetime time) { if (time.dayofweek != day) homecoming false; homecoming hours[time.hour]; } }

filter datetimes in range easy (i have alread weekpattern pattern object)

double[] info = { 1, 2, 3, 4} string[] times = { "23/01/2013 12:00", "23/01/2013 13:00", "23/01/2013 14:00", "23/01/2013 15:00" } list<datetime> datetimes = array.convertall(_times, time => datetime.parseexact(time, "dd/mm/yyyy hh:mm:ss", null)).tolist(); weekpattern pattern... // weekpattern object list<datetime> filter = datetimes.where(i => pattern.isinrange(i)).tolist();

but, how info filteres (double[] info filtered) instead of datetimes list of datetimes filtered?

1 collected on 23/01/2013 12:00 2 collected on 23/01/2013 13:00 3 collected on 23/01/2013 14:00 4 collected on 23/01/2013 15:00

suppose have range "wednesday, 13:00 - 14:00". want array of doubles 2 , 3:

data = { 2, 3 }

you can seek (this overload of select method accepts element index):

var filtereddata = datetimes.select((date, i) => { if (pattern.isinrange(date)) { homecoming data[i]; } else { homecoming -1; } });

the problem i'll need verify if value equals -1. works me.

editing: improve solution using method overload uses element index on lambda expression:

var filtereddata = data.where((d, i) => { homecoming pattern.isinrange(datetimes[i]); });

c# .net arrays linq

No comments:

Post a Comment