C#3.0をお持ちでない場合は、拡張メソッドがありません。
.NET3.5を使用していない場合、静的として呼び出すLinq拡張メソッドはありません。
あなたは機能のこれらの作品のかなりの数のために独自に追加することができます
public static IEnumerable<T> Distinct(IEnumerable<T> src, IEqualityComparer<T> eCmp)
{
Dictionary<T, bool> fakeHashSet = new Dictionary<T, bool>(eCmp);
//When I coded for 2.0 I had my own custom HashSet<T>, but that's overkill here
bool dummy;
foreach(T item in src)
{
if(!fakeHashSet.TryGetValue(item, out dummy))
{
fakeHashSet.Add(item, true);
yield return item;
}
}
}
public static IEnumerable<T> Distinct(IEnumerable<T> src)
{
return Distinct(src, EqualityComparer<T>.Default);
}
public delegate TResult Func<T, TResult>(T arg);//we don't even have this :(
public static int Count(IEnumerable<T> src, Func<T, bool> predicate)
{
int c = 0;
foreach(T item in src)
if(predicate(item))
++c;
return c;
}
我々は、拡張構文、またはlamdbasを持っていないので、我々は同じようにそれらを呼び出す必要があります:
foreach (string value in Distinct(list))
{
System.Diagnostics.Debug.WriteLine("\"{0}\" occurs {1} time(s).", value, Count(list, delegate(string v){return v == value;}));
}
C#2.0で多くのLinq-to-objectを実装することができますが、多くの人がやりましたが、それほどフレンドリーではありません。もちろん、他のクエリプロバイダにはマッピングできません。この場合
は、しかし、あなたは速いだけの直接のカウントをやっていると思います:
Dictonary<string, int> counts = new Dictionary<string, int>();
foreach(string value in list)
{
if(counts.ContainsKey(value))
counts[value]++;
else
counts[value] = 1;
}
foreach(KeyValuePair<string, int> kvp in counts)
System.Diagnostics.Debug.WriteLine("\"{0}\" occurs {1} time(s).", kvp.Key, kvp.Value));
をあなたが3.0よりも古いものを使用している場合、それは – jolySoft
で、あなたがアクセスすることはできませんどのようなネットのバージョンDistinctの拡張メソッドに変換します。 –
あなたの期待している結果は何ですか?代わりにどのような結果が表示されますか? – jrummell