简体   繁体   中英

C# How to use FirstOrDefault with an array while unable to have System.Linq in the namespace?

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

You can implement FirstOrDefault yourself or take it from the Reference Source :

public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
     if (source == null) throw ArgumentException("source");
     if (predicate == null) throw ArgumentException("predicate");

     foreach (TSource element in source) 
     {
        if (predicate(element)) 
        return element;
     }

     return default(TSource);
}

避免 linq 并使用数组作为第一个或默认值的最简单方法是 Array.Find(array, predicate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM