asp.net mvc - How to convert EF List objects to an arrayList of type dateTime -


i'm working on leave application. need excluded dates , convert datetime array.

i used following approach partially achieve this. created helper folder added following class folder:

namespace eleave.helpers {     public class businessdayscalculator     {         public int calculate(datetime startdate, datetime enddate, datetime[] holidays)         {             startdate = fixstartdate(startdate);             enddate = fixenddate(enddate.date);              if (startdate > enddate)                 return 0;             else                 return calculatedifference(startdate, enddate, holidays);         }            private int calculatedifference(datetime startdate, datetime enddate, datetime[] holidays)         {             int difference = (int)(enddate - startdate).totaldays;             int weeks = difference / 7;             if (enddate.dayofweek < startdate.dayofweek) weeks++;             difference -= weeks * 2;             difference -= holidaycount(startdate, enddate, holidays);             return difference + 1;         }            private int holidaycount(datetime startdate, datetime enddate, datetime[] holidays)         {             int holidaycount = 0;             foreach (datetime holiday in holidays)             {                 if (holidayinrange(startdate, enddate, holiday))                     holidaycount++;             }             return holidaycount;         }            private datetime fixstartdate(datetime date)         {             if (date.dayofweek == dayofweek.sunday)                 date = date.adddays(1);             else if (date.dayofweek == dayofweek.saturday)                 date = date.adddays(2);             return date.date;         }            private bool holidayinrange(datetime startdate, datetime enddate, datetime holiday)         {             return (holiday >= startdate                 && holiday <= enddate                 && holiday.dayofweek != dayofweek.saturday                 && holiday.dayofweek != dayofweek.sunday);         }            private datetime fixenddate(datetime date)         {             if (date.dayofweek == dayofweek.sunday)                 date = date.adddays(-2);             else if (date.dayofweek == dayofweek.saturday)                 date = date.adddays(-1);             return date.date;         }     } } 

i instantiated class below , passed excludeddate below:

// excluded dates var excludeddates = _context.excludeddate.where(d=>d.excludeddates >= startdate).where(d=>d.excludeddates <= enddate).toarray();  businessdayscalculator calc = new businessdayscalculator(); int days = calc.calculate(startdate, enddate, excludeddates);     

the implementation meant work gave errors when tried holidays datetime arrray below

   datetime[] holidays = new datetime[3];    holidays[0] = new datetime(2017, 08, 15);    holidays[1] = new datetime(2017, 09, 1);     

i got leave days excluded date in holiday array above

i need values of excludeddates , use class work.

the errors got here below:

error   1   best overloaded method match 'eleave.helpers.businessdayscalculator.calculate(system.datetime, system.datetime, system.datetime[])' has invalid arguments  c:\apps\webapplications\eleave\eleave\eleave\controllers\requestcontroller.cs   100 32  eleave   error   2   argument 3: cannot convert 'eleave.models.excludeddate[]' 'system.datetime[]'   c:\apps\webapplications\eleave\eleave\eleave\controllers\requestcontroller.cs   100 67  eleave 

model excluded date follows:

public class excludeddate {     public byte id { get; set; }     public datetime? excludeddates { get; set; } } 

your query returning array of excludeddate class not type datetime - need .select() clause select excludeddates property of object.

change query to

datetime[] excludeddates = _context.excludeddate     .where(d=>d.excludeddates >= startdate && d.excludeddates <= enddate)     .select(x => x.excludeddates)     .toarray(); 

note unclear why property nullable (there no point having null value property, , name little confusing in relationship table name. suggest change public datetime date { get; set; }


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -