Linq .Select with foreach

本文关键字:foreach with Select Linq | 更新日期: 2023-09-27 18:09:25

我需要一些帮助从一个系统。net转换。将Linq方法转换为非Linq版本。

For function Select():
 private readonly Dictionary<int, string> _foobarKeys = new Dictionary<int, string>();
 public IEnumerator<KeyValuePair<string, object>> GetFoobarator()
        {
            return _foobarKeys.Values
                               .Select(k => new KeyValuePair<string, object>(k, _dict[k]))
                               .GetFoobarator();
        }

同样的问题是与函数Max():

var maxKeyIndex = _foobarKeys.Keys.Max();

…任何():

_foobarKeys.Any(kv => kv.Value == key)

和FirstOrDefault ():

 private readonly Dictionary<string, object> _bar = new Dictionary<string, object>();
public bool Remove(string key)
        {
            var fooIndex = _foobarKeys.FirstOrDefault(kv => kv.Value == key)
                                       .Key;
            _foobarKeys.Remove(fooIndex);
            return _bar.Remove(key);
        }

谢谢你的帮助!

Linq .Select with foreach

使用内置的LINQ扩展方法有什么问题?

总之,这里有一些可能的实现:


选择

将序列中的每个元素投影成一个新的形式。

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> sequence, Func<TSource, TResult> selector)
{
    if (sequence == null) throw new ArgumentNullException("sequence");
    if (selector == null) throw new ArgumentNullException("selector");
    foreach (TSource element in sequence)
       yield return selector(element);
}

Max

返回泛型序列的最大值。

public static TSource Max<TSource>(this IEnumerable<TSource> sequence)
{
    if (sequence == null) throw new ArgumentNullException();
    Comparer<TSource> comparer = Comparer<TSource>.Default;
    TSource value = default(TSource);
    if (value == null)
    {
        foreach (TSource elem in sequence)
             if (elem != null && (elem == null || comparer.Compare(elem, value) > 0))
                   value = elem;
        return value;
    }
    else
    {
        bool hasValue = false;
        foreach (TSource elem in sequence)
        {
            if (hasValue)
            {
                if (comparer.Compare(elem, value) > 0)
                   value = elem;
            }
            else
            {
                    value = elem;
                    hasValue = true;
            }
        }
        if (!hasValue) // No elements in sequence
            throw new InvalidOperationException();
        return value;
    }
}

确定序列的任何元素是否满足条件。

public static bool Any<TSource>(this IEnumerable<TSource> sequence, Func<TSource, bool> predicate)
{
    if (sequence == null) throw new ArgumentNullException("sequence");
    if (predicate == null) throw new ArgumentNullException("predicate");
    foreach (TSource elem in sequence)
       if (predicate(elem))
          return true;
    return false;
}

FirstOrDefault

返回序列中满足条件的第一个元素,如果没有找到,则返回默认值。

 public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> sequence, Func<TSource, bool> predicate)
 {
     if (sequence == null) throw new ArgumentNullException("sequence");
     if (predicate == null) throw new ArgumentNullException("predicate");
     foreach (TSource elem in sequence)
         if (predicate(elem))
             return elem;
     return default(TSource);
 }