c# - Is it possible to make 1 generic method out of these 2 linq statements? -
i have 2 linq statements - in middle of switch block. statements below.
pwstarttime = lender.applicationwindows .where(w => w.name == pwperiod && w.endtime.timeofday > datetocheck.timeofday) .select(w => w.starttime) .firstordefault(); pwstarttime = lender.transferwindows .where(w => w.name == pwperiod && w.endtime.timeofday > datetocheck.timeofday) .select(w => w.starttime) .firstordefault();
as can see difference refer 2 different properties of "lender", however, elements used in linq query identical in "applicationwindows" , "transferwindows", although not same objects , each contain other unique properties.
so, it possible return w.startdate via 1 generic method?
thanks in advance.
// here's 2 classes
public class applicationwindow { public string name { get; set; } public datetime starttime { get; set; } public datetime endtime { get; set; } } public class transferwindow { public string name { get; set; } public datetime starttime { get; set; } public datetime endtime { get; set; } [xmlignore] public timespan cycle { get; set; } [browsable(false)] [xmlelement("cycle")] public string cyclestring { { return xmlconvert.tostring(cycle); } set { cycle = value.isnullorempty() ? timespan.zero : xmlconvert.totimespan(value); } } }
why not create interface or abstract base class transferwindow
, applicationwindow
, defining common properties? can (assuming lender.transferwindows
, lender.applicationwindows
lists of respective classes - if not, modify method signature appropriate):
public datetime getstarttime(ilist<window> windows, datetime datetocheck, string pwperiod) { return windows .where(w => w.name == pwperiod && w.endtime.timeofday > datetocheck.timeofday) .select(w => w.starttime) .firstordefault(); }
Comments
Post a Comment