通过在Linq中使用匿名类型进行分组 [英] group by using anonymous type in Linq

查看:129
本文介绍了通过在Linq中使用匿名类型进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Employee类,并且GetAllEmployees()返回一个雇员实例列表.我想按部门和性别对员工进行分组,所以我的答案是

Let say I have an Employee class, and GetAllEmployees() return a list of employee instance. I want to group employees by Department and Gender, so the answer I have is

var employeeGroup = Employee.GetAllEmployees()
                            .GroupBy(x => new { x.Department, x.Gender }) // I don't understand this anonymous type
                            .OrderBy(g => g.Key.Department)
                            .ThenBy(g => g.Key.Gender)
                            .Select(g => new {   //I can understand this anonymous type
                               Dept = g.Key.Department,
                               Gender = g.Key.Gender,
                               Employees = g.OrderBy(x => x.Name)
                             });

我有两个问题:

  1. 为什么匿名类型允许使用多个键分组?

  1. Why an anonymous type allows a multiple keys group by?

我不了解第一个匿名类型,因为据我了解,匿名类型的格式应为

I don't understand the first anonymous type, because from my understanding, an anonymous type's format should be like this

new {field1 = x.Department,field2 = x.Gender}

new { field1 = x.Department, field2 = x.Gender }

为什么没有字段的第一个匿名类型怎么可能?我的意思是,写这样的东西是正确的语法:

How come the first anonymous type can have without fields? I mean, it's correct syntax to write something like this:

var anonymous = new {field1 = 1,field2 =2}

但是如果我这样写,就会出现编译错误:

But there will be compile error if I write it like this:

var anonymous = new {1, 2} //compile error !!!

推荐答案

在这里可以使用匿名类型对多个字段进行分组,因为GroupBy使用默认的相等比较器.
匿名类型的默认相等比较器为匿名类型的每个属性使用默认相等比较器.

Anonymous types can be used here to group by multiple fields, because GroupBy uses the default equality comparer.
The default equality comparer for anonymous types uses the default equality comparer for each property of the anonymous type.

因此对于第一个匿名类型,如果两个Department和两个Gender都相等(根据它们的默认相等比较器),则两个实例相等.

So for the first anonymous type, two instances are equal if both Departments and both Genders are equal (according to their default equality comparers).

您可以想象匿名类型是这样的:

You can imagine the anonymous type being something like that:

public class AnonymousType1
{
    public int Department { get; set; } // I don't know your department type
    public int Gender { get; set; } // neither your gender type

    public int GetHashCode() { return Department.GetHashCode() ^ Gender.GetHashCode(); }
    public bool Equals(AnonymousType1 other)
    {
        if (ReferenceEquals(other, null)) return false;
        return Department == other.Department && Gender == other.Gender;
    }
}


第二个问题也很容易:编译器使用属性名称(x.Department中的Departmentx.Gender中的Gender)作为匿名类型的属性的名称.


The second question is easy, too: The compiler uses the property names (Department from x.Department and Gender from x.Gender) as names for the properties of the anonymous type.

所以

var anon = new { employee.Department, employee.Gender }

创建一个具有名为Department的属性和名为Gender的属性的类型.
当然,这只能与现有的属性/名称一起使用,而不能与诸如

creates a type with a property called Department and a property called Gender.
Of course this can only work with existing properties/names, not with constant values like

var anon = new {1,2}; // fails to compile, no names provided.

这篇关于通过在Linq中使用匿名类型进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆