C# 如何将LINQ与GROUP BY和multiple MAX(…)一起使用以选择多个字段?

C# 如何将LINQ与GROUP BY和multiple MAX(…)一起使用以选择多个字段?,c#,linq,linq-to-sql,fluent-nhibernate,linq-to-nhibernate,C#,Linq,Linq To Sql,Fluent Nhibernate,Linq To Nhibernate,我需要从具有多个where子句的表中的唯一记录中选择字段。我目前正在使用C和linqfluent语法与NHibernate连接。所以我想知道是否有一种方法可以通过这种方式创建这个查询。以下是一个测试数据集: +----+----------+------+------+ | Id | ParentId | Name | Type | +----+----------+------+------+ | 1 | 100 | A | 1 | | 2 | 100 |

我需要从具有多个where子句的表中的唯一记录中选择字段。我目前正在使用C和linqfluent语法与NHibernate连接。所以我想知道是否有一种方法可以通过这种方式创建这个查询。以下是一个测试数据集:

+----+----------+------+------+ | Id | ParentId | Name | Type | +----+----------+------+------+ | 1 | 100 | A | 1 | | 2 | 100 | A | 2 | | 3 | 100 | A | 3 | | 4 | 200 | B | 1 | | 5 | 300 | A | 1 | | 6 | 300 | A | 2 | | 7 | 400 | A | 1 | | 8 | 400 | A | 2 | | 9 | 400 | A | 3 | | 10 | 400 | A | 4 | +----+----------+------+------+ 结果如下:

+----------+-----+----+------+------+ | ParentId | Cnt | Id | Name | Type | +----------+-----+----+------+------+ | 100 | 3 | 3 | A | 3 | | 300 | 2 | 6 | A | 2 | | 400 | 4 | 10 | A | 4 | +----------+-----+----+------+------+
我知道如何通过查询进行分组,但我不知道如何进行多个最大选择。对林克来说这是不可能的吗?如果不是,那么我可以用什么方法来处理这个问题呢?

当您有一个可查询项时,您可以调用Select并传递一个谓词。new关键字使用您喜欢的模式构造对象

.Select( x => new
{
    ParentId = x.ParentId
    Cnt = x.Count(p => p.Name),
    Id = x.Max( p => p.Id  )
    /*etcetera*/
} );

当您有一个可查询项时,您可以调用Select并传递一个谓词。new关键字使用您喜欢的模式构造对象

.Select( x => new
{
    ParentId = x.ParentId
    Cnt = x.Count(p => p.Name),
    Id = x.Max( p => p.Id  )
    /*etcetera*/
} );

下面是一个小片段,向您展示了上下文中的linq查询:

public class Row
{
    public int Id;
    public int ParentId;
    public string Name;
    public int Type;

    public Row(int Id, int ParentId, string Name, int Type)
    {
        this.Id = Id;
        this.ParentId = ParentId;
        this.Name = Name;
        this.Type = Type;
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Row> test = new List<Row>();
        test.Add(new Row(1, 100, "A", 1));
        test.Add(new Row(2, 100, "A", 2));
        test.Add(new Row(3, 100, "A", 3));
        test.Add(new Row(4, 200, "B", 1));
        test.Add(new Row(5, 300, "A", 1));
        test.Add(new Row(6, 300, "A", 2));
        test.Add(new Row(7, 400, "A", 1));
        test.Add(new Row(8, 400, "A", 2));
        test.Add(new Row(9, 400, "A", 3));
        test.Add(new Row(10, 400, "A", 4));

        dynamic d = from row in test
                    where row.Name.Equals("A")
                    group row by row.ParentId into grp 
                    select new { 
                        ParentId = grp.Key, 
                        Cnt = grp.Count(), 
                        Id = grp.Max(x => x.Id), 
                        Name = grp.Max(x => x.Name), 
                        Type = grp.Max(x => x.Type) 
                    };
    }
}

下面是一个小片段,向您展示了上下文中的linq查询:

public class Row
{
    public int Id;
    public int ParentId;
    public string Name;
    public int Type;

    public Row(int Id, int ParentId, string Name, int Type)
    {
        this.Id = Id;
        this.ParentId = ParentId;
        this.Name = Name;
        this.Type = Type;
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Row> test = new List<Row>();
        test.Add(new Row(1, 100, "A", 1));
        test.Add(new Row(2, 100, "A", 2));
        test.Add(new Row(3, 100, "A", 3));
        test.Add(new Row(4, 200, "B", 1));
        test.Add(new Row(5, 300, "A", 1));
        test.Add(new Row(6, 300, "A", 2));
        test.Add(new Row(7, 400, "A", 1));
        test.Add(new Row(8, 400, "A", 2));
        test.Add(new Row(9, 400, "A", 3));
        test.Add(new Row(10, 400, "A", 4));

        dynamic d = from row in test
                    where row.Name.Equals("A")
                    group row by row.ParentId into grp 
                    select new { 
                        ParentId = grp.Key, 
                        Cnt = grp.Count(), 
                        Id = grp.Max(x => x.Id), 
                        Name = grp.Max(x => x.Name), 
                        Type = grp.Max(x => x.Type) 
                    };
    }
}

你能详细说明一下有一个构造函数吗?我尝试了这个,但我的对象上没有Max或Count函数。这只是一个POCO。@Jameshelly Max和Count可以从谓词中调用。我的答案开头的构造器写得不好,我会修改我的答案来修复它。你能详细说明一下有一个构造器吗?我尝试了这个,但我的对象上没有Max或Count函数。这只是一个POCO。@Jameshelly Max和Count可以从谓词中调用。我的答案开头的构造函数写得不好,我将编辑我的答案来修复它