6

経由で Web サービスから受信したデータがありますHTTPWebRequestNewtonSoft.Deserializeカスタム型 (パブリック文字列プロパティを持つ単純なクラス) を使用して解析した後、このデータを使用して操作したいLINQと考えています。より具体的には、データをグループ化したいと考えています。

私の問題は、単一の文字列プロパティでグループ化すると、グループ化が正常に機能することです

from x in myList
group x by x.myStr into grp
select grp;

より多くの列でグループ化したいので、カスタムタイプを返しています

new MyType { a = ..., b = ... }

しかし、グループは機能していません。その理由は、コンパイラがこれらのオブジェクトを比較する方法を知らないことに違いないと思ったので、この型が実装されていればIEqualityComparer<MyType>解決します。

いいえ、まだ適切にグループ化されておらず、まったく同じ文字列値を持つ複数のキーが作成されます。

私がグループ化しているカスタムタイプは次のようなものです

public class MyType
{
    public string a;
    public string b;
    public string c;
}

私が欠けているもののアイデアはありますか?

上記のシナリオの具体例を次に示します。

//The type that models the data returned from the web service
public class MyClass
{
    public string a { get; set; }

    public string b { get; set; }

    public string c { get; set; }

    public DateTime d { get; set; }

    public DateTime e { get; set; }
}

// the type by which I want to group my data
public class MyGroup : IEquatable<MyGroup>, IEqualityComparer<MyGroup>
{
    public string f1 { get; set; }

    public DateTime d1 { get; set; }

    public DateTime d2 { get; set; }

    public bool Equals(MyGroup other)
    {
        return string.Compare(this.f1, other.f1) == 0;
    }

    public bool Equals(MyGroup x, MyGroup y)
    {
        return string.Compare(x.f1, y.f1) == 0;
    }

    public int GetHashCode(MyGroup obj)
    {
        return obj.GetHashCode();
    }
}    
    List<MyClass> l = new List<MyClass>();
    l.Add(new MyClass { a = "aaa", b = "bbb", c = "ccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140101", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaa", b = "bbb", c = "ccc", d = DateTime.ParseExact("20090105", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140201", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aa", b = "bbbb", c = "cccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140201", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaa", b = "bbbbb", c = "ccc", d = DateTime.ParseExact("20121111", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140101", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaaa", b = "bbb", c = "ccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140101", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaa", b = "bbbbb", c = "ccc", d = DateTime.ParseExact("20121111", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140101", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaa", b = "bbbb", c = "cccccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140201", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaaa", b = "bbb", c = "cccc", d = DateTime.ParseExact("20090105", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140301", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaa", b = "bbb", c = "cccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140201", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });

    //The following does not really group
    //IEnumerable<IGrouping<MyGroup, MyClass>> r = from x in l
    IEnumerable<IGrouping<string, MyClass>> r = from x in l
                                                //group x by new MyGroup { f1 = x.a /*, d1 = x.d, d2 = x.e*/ } into grp
                                                orderby x.a
                                                group x by x.a into grp
                                                select grp;

    //foreach (IGrouping<MyGroup, MyClass> g in r)
    foreach (IGrouping<string, MyClass> g in r)
    {
        //Console.WriteLine(g.Key.f1);

        Console.WriteLine(g.Key);
    }
4

3 に答える 3

3

私が欠けているもののアイデアはありますか?

何かのようなもの:

public class MyType : IEquatable<MyType>
{
  public string a;
  public string b;
  public string c;

  public bool Equals(MyType other)
  {
    if (other == null)
      return false;

    if (GetType() != other.GetType()) // can be omitted if you mark the CLASS as sealed
      return false;

    return a == other.a && b == other.b && c == other.c;
  }

  public override bool Equals(object obj)
  {
    return Equals(obj as MyType);
  }

  public override int GetHashCode()
  {
    int hash = 0;
    if (a != null)
      hash ^= a.GetHashCode();
    if (b != null)
      hash ^= b.GetHashCode();
    if (c != null)
      hash ^= c.GetHashCode();
    return hash;
  }
}

追加:上記は変更可能であり、フィールドの1 つが再割り当てされるMyTypeとハッシュ コードが変更されることに注意してください。インスタンスがなどに保持されている間に再割り当てが発生した場合、これは問題です。abcDictionary<MyType, whatever>HashSet<MyType>


または、DavidGの回答で提案されているように、匿名型を「グループ化」するか、「グループ化」することもできますTuple.Create(.. , .. , ..)

于 2014-07-15T14:43:29.937 に答える