简体   繁体   English

如何在C#中创建具有多个group by子句的动态LINQ查询?

[英]How can I create a dynamic LINQ query in C# with possible multiple group by clauses?

I have been a programmer for some years now but I am a 我已经做了几年的程序员,但我是一个
newcomer to LINQ and C# so forgive me if my question sounds LINQ和C#的新人如果我的问题听起来请原谅我
particularly stupid. 特别愚蠢。

I hope someone may be able to point me in the right 我希望有人可以指出我的权利
direction. 方向。 My task is to come up with the ability to form a 我的任务是提出形成一个能力
dynamic multiple group by linq query within ac# script using 在ac#script中使用linq查询动态多组
a generic list as a source. 通用列表作为源。

For example, say I have a list containing multiple items with the following 例如,假设我有一个包含多个项目的列表,其中包含以下内容
structure: 结构体:

FieldChar1 - character
FieldChar2 - character
FieldChar3 - character
FieldNum1 - numeric
FieldNum2 - numeric

In a nutshell I want to be able to create a LINQ query that 简而言之,我希望能够创建一个LINQ查询
will sum FieldNum1 and FieldNum2 grouped by any one, two or 将FieldNum1和FieldNum2的总和除以任何一个,两个或
all three of the FieldChar fields that will be decided at 将决定的所有三个FieldChar字段
runtime depending on the users requirements as well as selecting the FieldChar fields in the same query. 运行时取决于用户要求以及在同一查询中选择FieldChar字段。

I have the dynamic.cs in my project which icludes a GroupByMany extension method but I have to admit I am really not sure how to put these to use. 我的项目中有dynamic.cs,它包含了一个GroupByMany扩展方法,但我不得不承认我真的不确定如何使用它们。 I am able to get the desired results if I use a query with hard-wired group by requests but not dynamically. 如果我按请求使用硬连线组但不动态查询,我能够获得所需的结果。

Apologies for any erroneous nomenclature, I am new to this language but any advice would be most welcome. 对任何错误的命名法道歉,我是这种语言的新手,但任何建议都会受到欢迎。

Many thanks 非常感谢

Alex 亚历克斯

Here is the basic GroupBy example. 这是基本的GroupBy示例。 Let's say we have a simple class like this: 假设我们有一个像这样的简单类:

public class Person
{
    public string Name { get; set; }

    public int Age { get; set; }

    public char Sex { get; set; }
}

Then, you can use GroupBy like this: 然后,您可以像这样使用GroupBy

var people = new List<Person> {
    new Person { Name = "Joe", Age = 30, Sex = 'M' },
    new Person { Name = "Liz", Age = 22, Sex = 'F' },
    new Person { Name = "Jim", Age = 22, Sex = 'M' },
    new Person { Name = "Alice", Age = 30, Sex = 'F' },
    new Person { Name = "Jenny", Age = 22, Sex = 'F' }
};
var groups = people.GroupBy(p => p.Age);

foreach (var group in groups)
{
    foreach (var person in group)
        Console.WriteLine(person.Name + " - " + person.Age);
}

As for grouping by multiple properties, see these: 至于多个属性的分组,请参阅以下内容:
http://weblogs.asp.net/zeeshanhirani/archive/2008/05/07/group-by-multiple-columns-in-linq-to-sql.aspx http://weblogs.asp.net/zeeshanhirani/archive/2008/05/07/group-by-multiple-columns-in-linq-to-sql.aspx
LINQ TO DataSet: Multiple group by on a data table LINQ TO DataSet:数据表上的多个分组

Basically, it is the same syntax with an anonymous type, for example: 基本上,它与匿名类型的语法相同,例如:

var groups = people.GroupBy(p => new { p.Age, p.Sex });

Note that you may be looking for using multiple OrderBy s: 请注意,您可能正在寻找使用多个OrderBy

var query = people.OrderBy(p => p.Age).ThenBy(p => p.Sex);
//You can have unlimited ThenBy's

Also note that the result of the last GroupBy statement and the result of this OrderBy statement is NOT the same. 另请注意,最后一个GroupBy语句的结果和此OrderBy语句的结果不一样。

GroupBy(groupByName + " " + groupByDirection, new {}) GroupBy(groupByName +“”+ groupByDirection,new {})

So you could pass in groupByName and groupByDirection, when you're stepping through, you might see something like 因此,您可以传入groupByName和groupByDirection,当您单步执行时,您可能会看到类似的内容

GroupBy("lastName desc", new{}) GroupBy(“lastName desc”,new {})

or 要么

GroupBy("firstName asc", new{}) GroupBy(“firstName asc”,new {})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM