Lambda表达式应用之排序分组

需求:
将城市列表按照城市名称的首字母分组并排序后输出。例如:
原数据源:

[
    {
        "CityId": 321,
        "CityName": "上海",
        "FullSpellName": "shanghai"
    },
    {
        "CityId": 226,
        "CityName": "苏州",
        "FullSpellName": "suzhou"
    },
    {
        "CityId": 53,
        "CityName": "北京",
        "FullSpellName": "beijing"
    },
    {
        "CityId": 383,
        "CityName": "杭州",
        "FullSpellName": "hangzhou"
    },
    {
        "CityId": 42,
        "CityName": "合肥",
        "FullSpellName": "hefei"
    },
    {
        "CityId": 229,
        "CityName": "无锡",
        "FullSpellName": "wuxi"
    }
]

目标数据源:

[
    {
        "FirstLetter": "B",
        "CityList": [
            {
                "CityId": 53,
                "CityName": "北京",
                "FullSpellName": "beijing",
                "FirstLetter": "B"
            }
        ]
    },
    {
        "FirstLetter": "H",
        "CityList": [
            {
                "CityId": 383,
                "CityName": "杭州",
                "FullSpellName": "hangzhou",
                "FirstLetter": "H"
            },
            {
                "CityId": 42,
                "CityName": "合肥",
                "FullSpellName": "hefei",
                "FirstLetter": "H"
            }
        ]
    },
    {
        "FirstLetter": "S",
        "CityList": [
            {
                "CityId": 321,
                "CityName": "上海",
                "FullSpellName": "shanghai",
                "FirstLetter": "S"
            },
            {
                "CityId": 226,
                "CityName": "苏州",
                "FullSpellName": "suzhou",
                "FirstLetter": "S"
            }
        ]
    },
    {
        "FirstLetter": "W",
        "CityList": [
            {
                "CityId": 229,
                "CityName": "无锡",
                "FullSpellName": "wuxi",
                "FirstLetter": "W"
            }
        ]
    }
]

按首字母排序,所以先给原数据源增加首字母字段:

List tempCityList = new List();
foreach (var item in CityList)
{
        CityEntity tempItem = new CityEntity();
        tempItem.FirstLetter = item.FullSpellName.Substring(0, 1).ToUpper();
        tempItem.CityId = item.CityId;
        tempItem.CityName = item.CityName;
        tempItem.FullSpellName= item.FullSpellName;
        tempCityList.Add(tempItem);
}

按照首字母排序:

var cityList = tempCityList.OrderBy(s => s.FirstLetter);

按照首字母分组:

List groupCitys = new List();
foreach (IGrouping group in cityList.GroupBy(s => s.FirstLetter))
{
    GroupCityList groupCity = new GroupCityList();
    groupCity.FirstLetter = group.Key;
    groupCity.CityList = group.ToList();

    #region 关键字筛选
    if (!string.IsNullOrEmpty(request.SearchKey) && groupCity.CityList != null && groupCity.CityList.Count > 0)
    {
        groupCity.CityList = groupCity.CityList.Where(s => s.CityName.Contains(request.SearchKey) || s.FullSpellName.Contains(request.SearchKey)).ToList();
    }
    #endregion

    groupCitys.Add(groupCity);
}

这个其实还蛮实用的,只要有类似的排序分组的需求,不一定非要是城市, 可以是其他的数据实体,都可以用这样的方式进行处理。遂作此文以记之。

参考:
OrderBY
GroupBy

----------------------------------------Update on 5 Feb 2018-----------------------------------------
还有一种方法可以分组排序,而且还不需要添加首字母字段,代码如下:

CityList.Sort(delegate(CityEntity city1,CityEntity city2){
  return city1.CityName.CompareTo(city2.CityName);
})

之前的方法用的是Linq表达式,但这个语法是C# 3.0才出现的,在有Linq之前,我们就是用delegate委托进行排序的,由此可以看出,lambda表达式本质上也是一种委托

你可能感兴趣的:(Lambda表达式应用之排序分组)