The code provided here works when I test it in the IDE, but the software itself which will be using this code doesn't give me the possibility declare using System.Linq
. I'm stuck trying to figure out how to use this else I'll have to go back to a solution I'd rather not use because it has a lower accuracy rate.
The issue is here
var stringMatch = sArray.FirstOrDefault(titleID.Contains);
I'm not sure how to properly provide the references. I think it should be something along the ways of System.Linq.Enumerable
... but it's the first time I'm dealing with such an issue so any help much appreciated.
string TakeEndPeriod = "";
string NewEndPeriod = "";
string FindEndSpace = "";
string GetEndPeriod = "";
string titleID = "document for the 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' does not contain a definition for 'FirstOrDefault' and no extension
method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be
found (are you missing a using directive or an assembly reference?)
EDIT:
This is the solution I got from the dev support managing the software:
String Title = "document for the period Ending December 31 2014";
Match M = Regex.Match(Title, ".*?(?i)(ended|ending)(.*)");//Case insensitive search for "ended" and "ending"
if (M.Groups.Count == 3)
{
//Match OK
DateTime DT = DateTime.Parse(M.Groups[2].Value);
//The variable DT will now have the parsed date.
Console.WriteLine(DT.ToString("yyyyMMdd"));
}
doesn't give me the possibility to declare 'using System.Linq'.
Since Enumerable.FirstOrDefault
is an extension method you can also use it's fully qualified class-name System.Linq.Enumerable
:
var stringMatch = System.Linq.Enumerable.FirstOrDefault(sArray, s => titleID.Contains(s));
or even shorter:
var stringMatch = System.Linq.Enumerable.FirstOrDefault(sArray, titleID.Contains);
Note that an extension method is simply a static method, that's why that works.
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With