C# 按字符串引用的属性动态分组

C# 按字符串引用的属性动态分组,c#,asp.net,linq,C#,Asp.net,Linq,因此,我有一个相对较大的数据集,我希望最终用户能够以某种方式进行自定义 当前数据分组如下: var versusHistories = from th in db.TeamHistories where th.TeamOneName.ToLower() == team.ToLower() group th by th.Date into grp1

因此,我有一个相对较大的数据集,我希望最终用户能够以某种方式进行自定义

当前数据分组如下:

var versusHistories =
                from th in db.TeamHistories
                where th.TeamOneName.ToLower() == team.ToLower()
                group th by th.Date
                into grp1
                from grp2 in
                    (from th in grp1
                        group th by th.Event)
                group grp2 by grp1.Key;
如果我想允许用户指定
group by
子句,我该怎么做

例如:

用户导航到

然后,该页面呈现以下代码:

var versusHistories =
                from th in db.TeamHistories
                where th.TeamOneName.ToLower() == team.ToLower()
                group th by th.TeamTwo
                into grp1
                from grp2 in
                    (from th in grp1
                        group th by th.Date)
                group grp2 by grp1.Key;

如何在不必硬编码每个可能性的情况下动态提供此功能?

使用方法语法,您可以执行以下操作:

var versusHistories =
            from th in db.TeamHistories
            where th.TeamOneName.ToLower() == team.ToLower()

if (condition)
{
    versusHistories = versusHistories.GroupBy(g => ..something..);
}
else if (anotherCondition)
{
    versusHistories = versusHistories.GroupBy(g => ..something else..);
}
我不确定使用纯查询语法是否/如何可能


这可以防止您多次重复核心查询。

使用方法语法,您可以执行以下操作:

var versusHistories =
            from th in db.TeamHistories
            where th.TeamOneName.ToLower() == team.ToLower()

if (condition)
{
    versusHistories = versusHistories.GroupBy(g => ..something..);
}
else if (anotherCondition)
{
    versusHistories = versusHistories.GroupBy(g => ..something else..);
}
我不确定使用纯查询语法是否/如何可能


这可以防止您多次重复核心查询。

这不是一个完整的答案,但您可以尝试使用反射并执行以下操作:

var listOfPropertiesToSortBy = new List<string>();
var versusHistories = from th in db.TeamHistories
                      where th.TeamOneName.ToLower() == team.ToLower();

foreach (var property in listOfPropertiesToSortBy ) {
       versusHistories = versusHistories.GroupBy(vh => typeof(TeamHistories).GetProperty(property).GetValue(vh));
}
var listofProperties ToSortby=new List();
var versusHistories=从数据库中的th开始
其中th.TeamOneName.ToLower()==team.ToLower();
foreach(ListofProperties ToSortby中的var属性){
versusHistories=versusHistories.GroupBy(vh=>typeof(TeamHistories).GetProperty(property).GetValue(vh));
}

问题在于,每次迭代列表时,新的OrderBy语句都会覆盖最后一个。需要有一种方式来表示。ThenBy在第一个属性之后,但我知道versusHistories=versusHistories。ThenBy(某物)无效。也许有人可以把它扩展一点,为你工作。希望这有帮助

这不是一个完整的答案,但您可以尝试使用反射并执行以下操作:

var listOfPropertiesToSortBy = new List<string>();
var versusHistories = from th in db.TeamHistories
                      where th.TeamOneName.ToLower() == team.ToLower();

foreach (var property in listOfPropertiesToSortBy ) {
       versusHistories = versusHistories.GroupBy(vh => typeof(TeamHistories).GetProperty(property).GetValue(vh));
}
var listofProperties ToSortby=new List();
var versusHistories=从数据库中的th开始
其中th.TeamOneName.ToLower()==team.ToLower();
foreach(ListofProperties ToSortby中的var属性){
versusHistories=versusHistories.GroupBy(vh=>typeof(TeamHistories).GetProperty(property).GetValue(vh));
}

问题在于,每次迭代列表时,新的OrderBy语句都会覆盖最后一个。需要有一种方式来表示。ThenBy在第一个属性之后,但我知道versusHistories=versusHistories。ThenBy(某物)无效。也许有人可以把它扩展一点,为你工作。希望这有帮助

因为有5+个可能的属性要分组,所以我会有~10个if子句。第一组5个,第二组5个。这看起来是不是太过分了?因为有5+个可能的属性要分组,所以我会有~10个if子句。第一组5个,第二组5个。这难道不过分吗?我相信我的要求是不可能的。在编写了与现有版本类似的版本后,它显示出
无法创建“System.Reflection.PropertyInfo”类型的常量值。在此上下文中仅支持基元类型或枚举类型。异常详细信息:System.NotSupportedException:无法创建“System.Reflection.PropertyInfo”类型的常量值。在此上下文中只支持基元类型或枚举类型。
您可能需要:.GroupBy(vh=>typeof(TeamHistories).GetProperty(property).GetValue(vh))我相信我所要求的是不可能的。在编写了与现有版本类似的版本后,它显示出
无法创建“System.Reflection.PropertyInfo”类型的常量值。在此上下文中仅支持基元类型或枚举类型。异常详细信息:System.NotSupportedException:无法创建“System.Reflection.PropertyInfo”类型的常量值。此上下文中仅支持基元类型或枚举类型。
您可能需要:.GroupBy(vh=>typeof(TeamHistories).GetProperty(property).GetValue(vh))签出不相似,但会告诉您如何实现它。签出不相似,但会告诉您如何实现它。