简体   繁体   中英

Why does this C# function exist in Visual Studio 2015 but not Visual Studio 2017

Just someone can explain me why this work on Visual Studio 2015 but not on Visual Studio 2017 ?

public static TConvert DynamicPop<TObject, TConvert>(this IEnumerable<TObject> obj, Converter<TObject, TConvert> converter, long @default = 1)
    {
        if (obj.Count() == 0)
        {
            dynamic _defaut = @default;
            return (TConvert)_defaut;
        }
        var collection = obj.ConvertAll<TConvert>(converter);
        collection.Sort();
        dynamic lastValue = collection.Last();
        return (TConvert)(lastValue + 1);
    }

That said to me that ConvertAll doesn't exist.

As mentioned in the comments, the method ConvertAll is defined on MSDN as a method of List<t> , not IEnumerable<T> . See here .

I can't tell you why this is working in Visual Studio 2015, but there is an easy fix to the code you can implement to make it work in Visual Studio 2017. Just change the convert line to:

var collection = obj.ToList().ConvertAll<TConvert>(converter);

Note that you'll need to have using System.Linq; at the top of your file.

ConvertAll is not an available method on IEnumerable<T> . In VS 2015 you must have been calling it via an extension method you built yourself (or which was referenced).

One option might be to replace:

var collection = obj.ConvertAll<TConvert>(converter);
collection.Sort();
dynamic lastValue = collection.Last();

with:

dynamic lastValue = obj.Select(z => converter(z)).OrderByDescending(z => z).First();

Basically just replacing ConvertAll with Select which does roughly the same thing. The OrderByDescending replaces the Sort also.

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