C# 自定义动态对象上的Linq按多个值分组
我制作了一个简单的动态对象:C# 自定义动态对象上的Linq按多个值分组,c#,linq,C#,Linq,我制作了一个简单的动态对象: class Row { Dictionary<string, object> properties = new Dictionary<string, object>(); private int rIndex; public Row(int rIndex) { this.rIndex = rIndex; } public object this[string name]
class Row
{
Dictionary<string, object> properties = new Dictionary<string, object>();
private int rIndex;
public Row(int rIndex)
{
this.rIndex = rIndex;
}
public object this[string name]
{
get
{
if (properties.ContainsKey(name))
{
return properties[name];
}
return null;
}
set
{
properties[name] = value;
}
}
public int RIndex
{
get { return rIndex; }
set { rIndex = value; }
}
}
这个很好用,但我觉得太静了
如何实现动态GROUPBY子句?
是否可以在GROUPBY子句中获取动态对象中的字典键值
提前感谢大家。好的,我认为像RowView类这样的东西可能是有意义的,它包含对行的引用,以及对行属性子集感兴趣的属性。然后,您可以使其实现IEquatable,从而使两个RowView对象相等,当且仅当: 它们以相同的顺序包含相同的属性 属性值在它们引用的行中是相同的 为方便起见,我可能会向行中添加CreateViewIEnumerable属性方法,以便您可以调用:
var t = lst.GroupBy(x => x.CreateView(groupingProperties));
我还建议为RIndex使用一个自动实现的属性来简化代码,并在理想情况下重命名它,因为目前还不清楚它的用途。你说的太静态是什么意思?您想如何表达您的分组?您从何处获得有关要分组依据的信息?要分组依据的信息是我从配置文件中获取的列名,这意味着总是不同的。好的,如果在问题中有这些信息,那就太好了。。。
var t = lst.GroupBy(x => x.CreateView(groupingProperties));