Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 这是我找到的最好的一个。。。很好,你把这归功于写这篇文章的人(尽管问这个问题的人很生气,但他没有应用) var numbers = new[] {1, 2, 3}; var squares = numbers.Select(i => i*i).ToA_C#_Linq_Arrays_Delegates_Projection - Fatal编程技术网

C# 这是我找到的最好的一个。。。很好,你把这归功于写这篇文章的人(尽管问这个问题的人很生气,但他没有应用) var numbers = new[] {1, 2, 3}; var squares = numbers.Select(i => i*i).ToA

C# 这是我找到的最好的一个。。。很好,你把这归功于写这篇文章的人(尽管问这个问题的人很生气,但他没有应用) var numbers = new[] {1, 2, 3}; var squares = numbers.Select(i => i*i).ToA,c#,linq,arrays,delegates,projection,C#,Linq,Arrays,Delegates,Projection,这是我找到的最好的一个。。。很好,你把这归功于写这篇文章的人(尽管问这个问题的人很生气,但他没有应用) var numbers = new[] {1, 2, 3}; var squares = numbers.Select(i => i*i).ToArray(); var squares = Array.ConvertAll(numbers, i => i*i); var numbers = new[] {new[] {1, 2}, new[] {3, 4}}; var squa

这是我找到的最好的一个。。。很好,你把这归功于写这篇文章的人(尽管问这个问题的人很生气,但他没有应用)
var numbers = new[] {1, 2, 3};
var squares = numbers.Select(i => i*i).ToArray();
var squares = Array.ConvertAll(numbers, i => i*i);
var numbers = new[] {new[] {1, 2}, new[] {3, 4}};
var squares = numbers.Select(i => i.Select(j => j*j).ToArray()).ToArray();
static Array ConvertAll<TSource, TResult>(this Array source,
                                          Converter<TSource, TResult> projection)
{
    if (!typeof (TSource).IsAssignableFrom(source.GetType().GetElementType()))
    {
        throw new ArgumentException();
    }
    var dims = Enumerable.Range(0, source.Rank)
        .Select(dim => new {lower = source.GetLowerBound(dim),
                            upper = source.GetUpperBound(dim)});
    var result = Array.CreateInstance(typeof (TResult),
        dims.Select(dim => 1 + dim.upper - dim.lower).ToArray(),
        dims.Select(dim => dim.lower).ToArray());
    var indices = dims
        .Select(dim => Enumerable.Range(dim.lower, 1 + dim.upper - dim.lower))
        .Aggregate(
            (IEnumerable<IEnumerable<int>>) null,
            (total, current) => total != null
                ? total.SelectMany(
                    item => current,
                    (existing, item) => existing.Concat(new[] {item}))
                : current.Select(item => (IEnumerable<int>) new[] {item}))
        .Select(index => index.ToArray());
    foreach (var index in indices)
    {
        var value = (TSource) source.GetValue(index);
        result.SetValue(projection(value), index);
    }
    return result;
}
var source = new int[2,3,4];

for (var i = source.GetLowerBound(0); i <= source.GetUpperBound(0); i++)
    for (var j = source.GetLowerBound(1); j <= source.GetUpperBound(1); j++)
        for (var k = source.GetLowerBound(2); k <= source.GetUpperBound(2); k++)
            source[i, j, k] = i*100 + j*10 + k;

var result = (int[,,]) source.ConvertAll<int, int>(i => i*i);

for (var i = source.GetLowerBound(0); i <= source.GetUpperBound(0); i++)
    for (var j = source.GetLowerBound(1); j <= source.GetUpperBound(1); j++)
        for (var k = source.GetLowerBound(2); k <= source.GetUpperBound(2); k++)
        {
            var value = source[i, j, k];
            Debug.Assert(result[i, j, k] == value*value);
        }
var newArray = arr.Select(x => myMethod(x)).ToArray();
using System.Linq;
 var squares = numberArray.Select(n => n * n).ToArray();
public static void ConvertInPlace<T>(this IList<T> source, Func<T, T> projection)
{
    for (int i = 0; i < source.Count; i++)
    {
        source[i] = projection(source[i]);
    }
}
int[] values = { 1, 2, 3 };
values.ConvertInPlace(x => x * x);
int[] values = { 1, 2, 3 };
values = Array.ConvertAll(values, x => x * x);
int[] x =  {1,2,3};
x = x.Select(( Y ) => { return Y * Y; }).ToArray();
    // credit: https://blogs.msdn.microsoft.com/ericlippert/2010/06/28/computing-a-cartesian-product-with-linq/
    public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
    {
        IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
        foreach (var sequence in sequences)
        {
            // got a warning about different compiler behavior
            // accessing sequence in a closure
            var s = sequence;
            result = result.SelectMany(seq => s, (seq, item) => seq.Concat<T>(new[] { item }));
        }
        return result;
    }


    public static void ConvertInPlace(this Array array, Func<object, object> projection)
    {
        if (array == null)
        {
            return;
        }

        // build up the range for each dimension
        var dimensions = Enumerable.Range(0, array.Rank).Select(r => Enumerable.Range(0, array.GetLength(r)));

        // build up a list of all possible indices
        var indexes = EnumerableHelper.CartesianProduct(dimensions).ToArray();

        foreach (var index in indexes)
        {
            var currentIndex = index.ToArray();
            array.SetValue(projection(array.GetValue(currentIndex)), currentIndex);
        }
    }