C# Linq.使用foreach选择

C# Linq.使用foreach选择,c#,.net,linq,C#,.net,Linq,我需要一些帮助,将.Net从System.Linq方法转换为非Linq版本 对于Funktion Select(): 。。。Any(): 和FirstOrDefault(): private readonly Dictionary\u bar=new Dictionary(); 公共布尔删除(字符串键) { var foodindex=\u foobarKeys.FirstOrDefault(kv=>kv.Value==key) 钥匙 _去除(食物索引); 返回条。移除(键); } 谢谢你的帮

我需要一些帮助,将.Net从System.Linq方法转换为非Linq版本

对于Funktion Select():

。。。Any():

和FirstOrDefault():

private readonly Dictionary\u bar=new Dictionary();
公共布尔删除(字符串键)
{
var foodindex=\u foobarKeys.FirstOrDefault(kv=>kv.Value==key)
钥匙
_去除(食物索引);
返回条。移除(键);
}

谢谢你的帮助

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

无论如何,以下是可能的实现:


挑选 将序列中的每个元素投影到新形式中

公共静态IEnumerable选择(此IEnumerable序列,Func选择器)
{
如果(sequence==null)抛出新的ArgumentNullException(“sequence”);
如果(选择器==null)抛出新的ArgumentNullException(“选择器”);
foreach(序列中的TSource元素)
收益率返回选择器(元素);
}
马克斯 返回泛型序列中的最大值

publicstatictsourcemax(这个IEnumerable序列)
{
如果(sequence==null)抛出新的ArgumentNullException();
Comparer=Comparer.Default;
TSource值=默认值(TSource);
如果(值==null)
{
foreach(按顺序的t源元素)
if(elem!=null&(elem==null | | comparer.Compare(elem,value)>0))
值=元素;
返回值;
}
其他的
{
bool hasValue=false;
foreach(按顺序的t源元素)
{
if(hasValue)
{
如果(比较器比较(元素,值)>0)
值=元素;
}
其他的
{
值=元素;
hasValue=true;
}
}
if(!hasValue)//序列中没有元素
抛出新的InvalidOperationException();
返回值;
}
}
任何 确定序列的任何元素是否满足条件

公共静态bool Any(此IEnumerable序列,Func谓词)
{
如果(sequence==null)抛出新的ArgumentNullException(“sequence”);
如果(谓词==null)抛出新的ArgumentNullException(“谓词”);
foreach(按顺序的t源元素)
if(谓词(elem))
返回true;
返回false;
}
第一默认值 返回序列中满足条件的第一个元素,如果未找到此类元素,则返回默认值

public static TSource FirstOrDefault(此IEnumerable序列,Func谓词)
{
如果(sequence==null)抛出新的ArgumentNullException(“sequence”);
如果(谓词==null)抛出新的ArgumentNullException(“谓词”);
foreach(按顺序的t源元素)
if(谓词(elem))
返回元素;
返回默认值(TSource);
}

您为什么需要这个?您是否正在移植到不支持LINQ的.NET Framework的旧版本?如果是,什么版本?你可以看看Jon Skeet的。它展示了如何实现所有linq方法。你的问题是什么?
 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();
        }
var maxKeyIndex = _foobarKeys.Keys.Max();
_foobarKeys.Any(kv => kv.Value == key)
 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);
        }
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);
}
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;
}
 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);
 }