我有一个示例数据
---------------------------------------
Name | Old | Year | Address
---------------------------------------
1. Ann | 24 | 1990 | Address1
---------------------------------------
2. Billy | 22 | 1992 | Address2
---------------------------------------
3. Chris | 24 | 1990 | Address3
---------------------------------------
4. Dean | 23 | 1991 | Address4
---------------------------------------
数据将显示在网格上,如标题列:名称,旧,年份,地址
数据类型:IList。每个项目都有类型:object []
如果我将名称分组,则转为:
---------------------------------------
Name | Old | Year | Address
---------------------------------------
1. Ann | 24 | 1990 | Address1
---------------------------------------
2. Billy | 22 | 1992 | Address2
---------------------------------------
3. Chris | 24 | 1990 | Address3
---------------------------------------
4. Dean | 23 | 1991 | Address4
---------------------------------------
如果按年份分组,则转为:
------------------------------------------------------
Name | Old | Year | Address
------------------------------------------------------
1. Ann, Chris | 24 | 1990 | Address1, Address3
------------------------------------------------------
2. Billy | 22 | 1992 | Address2
------------------------------------------------------
3. Dean | 23 | 1991 | Address4
------------------------------------------------------
所以LINQ支持的当前组,它不能那样做。 我的方法是:先按年份分组,之后,我手动操作数据。 我有用户想要分组的列表索引:IList,在这种情况下将是2。
因为我的数据有IList并且每个都是object [],所以我写了一个这样的类:
class GroupComparer : IEqualityComparer<object>
{
private IList<int> indexList;
public GroupComparer(IList<int> indexList)
{
this.indexList = indexList;
}
#region IEqualityComparer<object> Members
public bool Equals(object x, object y)
{
object[] arrX = (object[])x;
object[] arrY = (object[])y;
bool isEqual = true;
foreach (var item in this.indexList)
{
if(arrX[item] != arrY[item])
{
isEqual = false;
}
}
return isEqual;
}
public int GetHashCode(object obj)
{
int hCode = 0;
object[] arrObj = (object[])obj;
foreach (var item in this.indexList)
{
hCode ^= (int)arrObj[item];
}
return hCode.GetHashCode();
}
#endregion
}
这个地方消耗了它:
IList<object> data = ConvertToArrayObject(strDatas);
IList<int> index = new List<int>();
index.Add(2);
var groups = data.GroupBy(p => p, new GroupComparer(index));
但结果不是小组。
您可以告诉我如何按索引进行分组吗?
最好的关注