我有通过HTTPWebRequest
. 在我将其解析NewtonSoft.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);
}