C# 具有动态groupBy的动态对象列表

C# 具有动态groupBy的动态对象列表,c#,linq,C#,Linq,我需要实现这样的目标,但我不知道如何实现: class Program { static List<dynamic> myList = new List<dynamic>(); static void Main(string[] args) { myList.Add(new { Test = "test", Test2 = "test2" }); myList.Add(new { Test = "test", Te

我需要实现这样的目标,但我不知道如何实现:

class Program
{
    static List<dynamic> myList = new List<dynamic>();

    static void Main(string[] args)
    {
        myList.Add(new { Test = "test", Test2 = "test2" });
        myList.Add(new { Test = "test", Test2 = "test2" });
        myList.Add(new { Test = "test1", Test2 = "test2" });

        // I need to pass a dynamic list of expression like:
        GenerateGroup<dynamic>(x=>x.Test, x => x.Test2); //groups myList in 2 groups
        GenerateGroup<dynamic>(x=>x.Test2); //groups myList in a single group
    }

    private static void GenerateGroup<T>(params Expression<Func<T, object>>[] properties)
    {
       //I need to group for the properties passed as parameters
       var result= myList.GroupBy(properties);
    }
}
类程序
{
静态列表myList=新列表();
静态void Main(字符串[]参数)
{
添加(新的{Test=“Test”,Test2=“Test2”});
添加(新的{Test=“Test”,Test2=“Test2”});
添加(新的{Test=“test1”,Test2=“Test2”});
//我需要传递表达式的动态列表,如:
GenerateGroup(x=>x.Test,x=>x.Test2);//将我的列表分为两组
GenerateGroup(x=>x.Test2);//在单个组中对我的列表进行分组
}
私有静态void生成器组(参数表达式[]属性)
{
//我需要对作为参数传递的属性进行分组
var result=myList.GroupBy(属性);
}
}
我收到编译器错误:

表达式树不能包含动态操作

这只是一个复杂应用程序的示例,但最后,我需要使用一个动态列表,并使用一个动态属性列表对它们进行分组。这是因为我从多个PDF源读取数据/属性,不可能使用静态类

是否有可能修复此错误,或者这是编译器的限制,我需要以另一种方式处理此问题

更新

多亏了你的回答,我想我已经向前迈出了一步:

class Program
{
    static List<dynamic> myList = new List<dynamic>();

    static List<Foo> myListFoo = new List<Foo>();


    static void Main(string[] args)
    {
        myList.Add(new { Test = "test", Test2 = "test2" });
        myList.Add(new { Test = "test", Test2 = "test2" });
        myList.Add(new { Test = "test1", Test2 = "test2" });


        myListFoo.Add(new Foo { MyProperty =1});

        GenerateGroup<dynamic>(x =>new  {  x.Test,  x.Test2});

        GenerateGroup<Foo>(x=>x.MyProperty);
    }

    private static void GenerateGroup<T>(Func<T, object> properties)
    {
       var result= myList.GroupBy(properties);

        result = myListFoo.GroupBy(properties);
    }
}

public class Foo
{
    public int MyProperty { get; set; }
}
类程序
{
静态列表myList=新列表();
静态列表myListFoo=新列表();
静态void Main(字符串[]参数)
{
添加(新的{Test=“Test”,Test2=“Test2”});
添加(新的{Test=“Test”,Test2=“Test2”});
添加(新的{Test=“test1”,Test2=“Test2”});
Add(newfoo{MyProperty=1});
GenerateGroup(x=>new{x.Test,x.Test2});
GenerateGroup(x=>x.MyProperty);
}
专用静态void生成器组(Func属性)
{
var result=myList.GroupBy(属性);
结果=myListFoo.GroupBy(属性);
}
}
公开课Foo
{
公共int MyProperty{get;set;}
}
我在groupBy上只有一个编译错误:

List
”不包含“GroupBy”和最佳扩展方法重载“
parallelenumable”的定义。GroupBy
ParallelQuery,Func
)需要类型为“
ParallelQuery
”的接收器


您需要对代码进行一些更改:

  • Expression
    不是您要查找的内容,
    Where
    重载被
    IEnumerable
    接受,全部使用
    Func
  • 如果要使用硬编码
    动态
  • 您不应该传递多个参数,而应该创建一个包含要按其分组的数据的对象
如果您将所有这些点混合在一起,您应该得到如下结果:

static void Main(string[] args)
{
    myList.Add(new { Test = "test", Test2 = "test2" });
    myList.Add(new { Test = "test", Test2 = "test2" });
    myList.Add(new { Test = "test1", Test2 = "test2" });

    // I need to pass a dynamic list of expression like:
    GenerateGroup(x => new { x.Test, x.Test2 } ); //groups myList by Test and Test2
    GenerateGroup(x => x.Test2); //groups myList in a single group
}

private static void GenerateGroup(Func<dynamic, object> groupper)
{
    var groupped = myList.GroupBy(groupper);
}
static void Main(字符串[]args)
{
添加(新的{Test=“Test”,Test2=“Test2”});
添加(新的{Test=“Test”,Test2=“Test2”});
添加(新的{Test=“test1”,Test2=“Test2”});
//我需要传递表达式的动态列表,如:
GenerateGroup(x=>new{x.Test,x.Test2});//按Test和Test2对myList进行分组
GenerateGroup(x=>x.Test2);//在单个组中对我的列表进行分组
}
专用静态无效生成器组(Func groupper)
{
var groupped=myList.GroupBy(groupper);
}

不清楚为什么要使用
动态
,或者表达式树,或者您期望的结果是什么。你能解释一下你想要实现的目标吗?我解释了代码,希望它现在清楚了。即使你使用静态类型而不是动态类型,你的代码也不会编译。也许你应该先解决这个问题。很高兴认识卡米洛!我会调查的!谢谢!你的输入大概是动态的?源代码是什么,JSON文件还是什么?@danyolgiax很乐意帮忙!:)