LINQ Conversion Function

Only one curious question from me is if there is any linq conversion function. I mean, if I have List<int>or List<Foo>, I would like to change the elements at index x or anyone who satisfies the Where clause.

+3
source share
2 answers

If you want to conditionally project Foo onto another Foo (leaving the others untouched), you can do something like:

IEnumerable<Foo> foos = ...

var transformed = foos.Select(foo => myCondition(foo) ? transform(foo) : foo);

On the other hand, if you only want to design Foos that meet the condition:

var transformed = foos.Where(foo => myCondition(foo))
                      .Select(foo => transform(foo));

, - LINQ . , , .

// assuming the transform is from Foo -> Foo
foos = foos.Select(foo => transform(foo)).ToList();

, , LINQ, - List<T>.ConvertAll:

List<Foo> foos = ...

// implicitly List<Foo> assuming the transform is from Foo -> Foo
var transformed = foos.ConvertAll
                  (foo => myCondition(foo) ? transform(foo) : foo);

EDIT. , "ReplaceWhere" - , framework, . , :

/// <summary>
/// Replaces items in a list that match the specified predicate,
/// based on the specified selector. 
/// </summary>
public static void ReplaceWhere<T>(this IList<T> list,
                                   Func<T, bool> predicate,
                                   Func<T, T> selector)
{
    // null-checks here.

    for (int i = 0; i < list.Count; i++)
    {
        T item = list[i];

        if (predicate(item))
            list[i] = selector(item);
    }
}

List<int> myList = ...
myList.ReplaceWhere(i => i > 0, i => i * i);
+11

:

// allows you to transform every element
public static List<T> TransformAll<T>(this List<T> list,
                                      Func<T, T> converter)
{
    for (int i = 0; i < list.Count; i++)
    {
        list[i] = converter(list[i]);
    }
    return list;
}

// allows you to transform every element based on its index
public static List<T> TransformAll<T>(this List<T> list,
                                      Func<T, int, T> converter)
{
    for (int i = 0; i < list.Count; i++)
    {
        list[i] = converter(list[i], i);
    }
    return list;
}

// allows you to transform chosen elements
public static List<T> TransformWhere<T>(this List<T> list,
                                        Func<T, bool> predicate,
                                        Func<T, T> converter)
{
    for (int i = 0; i < list.Count; i++)
    {
        T item = list[i];
        if (predicate(item))
            list[i] = converter(item);
    }
    return list;
}

// allows you to transform chosen elements based on its index
public static List<T> TransformWhere<T>(this List<T> list,
                                        Func<T, int, bool> predicate,
                                        Func<T, int, T> converter)
{
    for (int i = 0; i < list.Count; i++)
    {
        T item = list[i];
        if (predicate(item, i))
            list[i] = converter(item, i);
    }
    return list;
}

, , , , list.TransformAll(x => x + 2).Sum().

+4

Source: https://habr.com/ru/post/1781579/


All Articles