0

Key と OtherThing.Id に参加したい辞書と IEnumerable があります。

これは、Xamarin Monodroid の単純な linq 結合で正常に機能します。次に、IO のアプリでコードを使用したいと考えました。例外がスローされ、その理由がすぐにわかりました。Xamarin Docs で説明されているように、値の型で Generic Dictionaries を使用することはできません。

次に、1.7 章の Xamarin ドキュメントで説明されている回避策を試し (ここで検索)、IEqualityComparer の独自の実装を Dictionary のコンストラクターに次のように渡します。

演算子:

public class EqualityComparer<T> : IEqualityComparer<T>
{
    #region IEqualityComparer implementation

    public bool Equals (T x, T y)
    {
        return x.Equals (y);
    }

    public int GetHashCode (T obj)
    {
        return obj.GetHashCode ();
    }

    #endregion

}

そして辞書に渡します

Dictionary<Int32, Attempt> allAttempts = 
new Dictionary<int, Attempt>(new EqualityComparer<int>());

allAttempts.Add (1, new Attempt () {Id=1, Name="attempt for 1"} );
allAttempts.Add (2, new Attempt () {Id=2, Name="attempt for 2"} );

しかし、エラーは結合で発生しているように見えるので、結合にも Comparer を提供しようとしました:

var result = conditions.Join(
attempts,
c => c.Id,
cA => cA.Key,
(c, cA) => cA.Value,
new EqualityComparer<Int32> ());

しかし、これでも次の例外が発生します。

got this: System.ExecutionEngineException: Attempting to JIT compile method         'System.Linq.Enumerable/Function1<System.Collections.Generic.KeyValuePair2>:m__56 (System.Collections.Generic.KeyValuePair`2)' while running with --aot-only. 
See http://docs.xamarin.com/ios/about/limitations for more information. 

at System.Linq.Enumerable.ToLookupKeyValuePair2,Int32,KeyValuePair2<IEnumerable%601%20source,%20System.Func%602%20keySelector,%20System.Func%602%20elementSelector,%20IEqualityComparer%601%20comparer> [0x00079]
in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:2977
at System.Linq.Enumerable.ToLookupKeyValuePair`2,Int32<IEnumerable%601%20source,%20System.Func%602%20keySelector,%20IEqualityComparer%601%20comparer> [0x00000] 
in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:2945 
at System.Linq.Enumerable+c__Iterator184[Drallo.ChallengeEngine.Condition,System.Collections.Generic.KeyValuePair2[System.Int32,Drallo.ChallengeEngine.Attempt.ConditionAttempt],System.Int32,Drallo.ChallengeEngine.Attempt.ConditionAttempt].MoveNext () [0x00023] 
in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:1157

ここで、この問題を再現するために設定した ios のテスト プロジェクトを見つけることができます: https://github.com/stestaub/TestLinqJoins/blob/master/TestLinqJoins/JoinWithDictionary.cs

この場合の問題は何ですか?どうすればこれを解決/回避できますか? 私はまだlinqを使用したいと思います。

助けとヒントをありがとう

4

0 に答える 0