简体   繁体   English

LINQ 查询 Grouping By 和 ordering

[英]LINQ query Grouping By and ordering

I have a API service like Google API working with query...Sometimes user type for example "Rio".我有一个 API 服务,如 Google API 使用查询......有时用户类型,例如“Rio”。 I have an Enumerable with object with fields我有一个带有字段的 object 的 Enumerable

        "city",
        "city_code"
        "country"
        "country_code"
        "rating"
        "type"
        "name"
        "code"

So far so good, I have to order it grouping by City.到目前为止一切顺利,我必须按城市分组订购。 Plus, if "type" is country it need to appear first than others with same city.另外,如果“类型”是国家,则它需要比具有相同城市的其他国家首先出现。

For example:例如:

{
    "city": "Rio De Janeiro",
    "city_code": "RIO",
    "country": "Brazil",
    "country_code": "BR",
    "rating": 3,
    "type": "airport",
    "name": "Santos Dumont",
    "code": "SDU"
},
{
    "city": "Rio De Janeiro",
    "city_code": "RIO",
    "country": "Brazil",
    "country_code": "BR",
    "rating": 2,
    "type": "airport",
    "name": "Galeao Antonio Carlos Jobim International",
    "code": "GIG"
},
{
    "country": "Brazil",
    "country_code": "BR",
    "type": "city",
    "name": "Rio De Janeiro",
    "code": "RIO"
}

In this case, I need to show the type "city" before the type "airport".在这种情况下,我需要在“机场”类型之前显示“城市”类型。 Sometimes the json come with a lot of data, but I cutted to show here.有时 json 带有很多数据,但我在这里截取了展示。

If you want to order by 2 properties you can use something like this:如果您想按 2 个属性订购,可以使用以下内容:

yourList.OrderBy(x => x.City).ThenBy(x => x.Type)

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

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