.net - C# How to use FirstOrDefault with an array while unable to have System.Linq in the namespace? -
the code provided here works when test in ide, software using code doesn't give me possibility declare using system.linq
. i'm stuck trying figure out how use else i'll have go solution i'd rather not use because has lower accuracy rate.
the issue here
var stringmatch = sarray.firstordefault(titleid.contains);
i'm not sure how provide references. think should along ways of system.linq.enumerable
... it's first time i'm dealing such issue appreciated.
string takeendperiod = ""; string newendperiod = ""; string findendspace = ""; string getendperiod = ""; string titleid = "document period ended december 31 2014"; string s1 = "ended "; string s2 = "ended "; string s3 = "ending "; string[] sarray = new [] { s1, s2, s3}; var stringmatch = sarray.firstordefault(titleid.contains); if (stringmatch != null) { takeendperiod = titleid.substring(titleid.lastindexof(stringmatch)); findendspace = takeendperiod.substring(takeendperiod.indexof(" ")); getendperiod = findendspace.substring(1); string[] formatarray = new[] { "dd mmmm yyyy", "mmmm dd yyyy" }; datetime modendperiod; if (!datetime.tryparseexact(getendperiod, formatarray, system.globalization.cultureinfo.invariantculture, system.globalization.datetimestyles.none, out modendperiod)) { //parsing failed } newendperiod = modendperiod.tostring("yyyy-mm-ddt00:00:00z");
edit:
error message i'm getting:
'system.array' not contain definition 'firstordefault' , no extension method 'firstordefault' accepting first argument of type 'system.array' found (are missing using directive or assembly reference?)
edit:
this solution got dev support managing software:
string title = "document period ending december 31 2014"; match m = regex.match(title, ".*?(?i)(ended|ending)(.*)");//case insensitive search "ended" , "ending" if (m.groups.count == 3) { //match ok datetime dt = datetime.parse(m.groups[2].value); //the variable dt have parsed date. console.writeline(dt.tostring("yyyymmdd")); }
doesn't give me possibility declare 'using system.linq'.
since enumerable.firstordefault
extension method can use it's qualified class-name system.linq.enumerable
:
var stringmatch = system.linq.enumerable.firstordefault(sarray, s => titleid.contains(s));
or shorter:
var stringmatch = system.linq.enumerable.firstordefault(sarray, titleid.contains);
note extension method static method, that's why works.
Comments
Post a Comment