【问题标题】:How use linq multi list criteria?如何使用 linq 多列表条件?
【发布时间】:2019-03-31 12:50:40
【问题描述】:

我想通过使用其他api根据条件订购多列表。

结果(我从其他 api 使用)

{
    "returned_data": {
        "data": [
            {
                "firstName": "FirstNameAA",
                "lastName": "LastNameAA",
                "product": [
                    {
                        "license": "1AS131",
                        "carType": "478",
                        "contract": "0112345",
                        "amounttoCurrent": 3000
                    }               
                ]
            },
            {
                "firstName": "FirstNameAA",
                "lastName": "LastNameAA",
                "product": [
                    {
                        "license": "2AS345",
                        "carType": "465",
                        "contract": "10234521",
                        "amounttoCurrent": 12000
                    }
                ]
            },
            {
                "firstName": "FirstNameBB",
                "lastName": "LastNameBB",
                "product": [
                    {
                        "license": "kdf9034",
                        "carType": "4234",
                        "contract": "8995412",
                        "amounttoCurrent": 1000
                    }
                ]
            }
        ]
    }
}

但我想要新结果,按“firstName”对每个列表进行新排序

{
    "returned_data": {
        "data": [
            {
                "firstName": "FirstNameAA",
                "lastName": "LastNameAA",
                "product": [
                    {
                        "license": "1AS131",
                        "carType": "478",
                        "contract": "0112345",
                        "amounttoCurrent": 3000
                    },
                    {
                        "license": "2AS345",
                        "carType": "465",
                        "contract": "10234521",
                        "amounttoCurrent": 12000
                    }
                ]
            },            
            {
                "firstName": "FirstNameBB",
                "lastName": "LastNameBB",
                "product": [
                    {
                        "license": "kdf9034",
                        "carType": "4234",
                        "contract": "8995412",
                        "amounttoCurrent": 1000
                    }
                ]
            }
        ]
    }
}

代码 c#

var newResult = resReturnListData.returned_data.data.GroupBy(x => x.firstName);  >>> not work.

请帮助我。非常感谢。

【问题讨论】:

  • 能否请您为您的 json 展示您的课程?
  • 是编译时错误还是您在 newResult 对象中没有得到任何结果?
  • 如果您想订购商品,那么您的 linq 应该显示OrderBy。请展示您尝试订购的商品。
  • 什么不起作用?您无法将 json 转换为类,或者您无法将转换后的类转换为您想要的结果?

标签: c# list linq api asp.net-core


【解决方案1】:

假设你的数据结构如下,

public class Product
{
    public string license { get; set; }
    public string carType { get; set; }
    public string contract { get; set; }
    public int amounttoCurrent { get; set; }
}

public class Datum
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public List<Product> product { get; set; }
}

public class ReturnedData
{
    public List<Datum> data { get; set; }
}

public class RootObject
{
    public ReturnedData returned_data { get; set; }
}

您可以通过 GroupBy 获取“数据”结果,然后使用匿名对象将其包裹起来。

var resReturnListData = JsonConvert.DeserializeObject<RootObject>(jsonString);
var newResult = resReturnListData.returned_data.data
                .GroupBy(x => x.firstName)
                .Select(x => new Datum
                {
                    firstName = x.Key,
                    lastName = x.Select(c => c.lastName).FirstOrDefault(),
                    product = x.SelectMany(c => c.product).ToList()

                });

var finalObject = new RootObject
{
    returned_data = new ReturnedData
    {
        data = newResult.ToList()
    }
};

var jsonResult = JsonConvert.SerializeObject(finalObject,Newtonsoft.Json.Formatting.Indented);

输出样本,

{
  "returned_data": {
    "data": [
      {
        "firstName": "FirstNameAA",
        "lastName": "LastNameAA",
        "product": [
          {
            "license": "1AS131",
            "carType": "478",
            "contract": "0112345",
            "amounttoCurrent": 3000
          },
          {
            "license": "2AS345",
            "carType": "465",
            "contract": "10234521",
            "amounttoCurrent": 12000
          }
        ]
      },
      {
        "firstName": "FirstNameBB",
        "lastName": "LastNameBB",
        "product": [
          {
            "license": "kdf9034",
            "carType": "4234",
            "contract": "8995412",
            "amounttoCurrent": 1000
          }
        ]
      }
    ]
  }
}

【讨论】:

    【解决方案2】:

    您需要使用适当的分组依据,然后从分组依据结果中仅选择product

    var newResult = resReturnListData.returned_data.data
        .GroupBy(x => x.firstName)
        .Select(g => new
        {
            firstName = g.Key,
            lastName = g.Select(x => x.lastName).FirstOrDefault(),
            product = g.SelectMany(x => x.product).ToList()
        }).ToList();
    

    如果您想使用 firstNamelastName 按数据分组,那么,

    var newResult = resReturnListData.returned_data.data
        .GroupBy(x => new { x.firstName, x.lastName })
        .Select(g => new
        {
            firstName = g.Key.firstName,
            lastName = g.Key.lastName,
            product = g.SelectMany(x => x.product).ToList()
        }).ToList();
    

    用法:

    string json = "Your json here";
    
    JObject jObject = JObject.Parse(json);
    
    RootObject resReturnListData = jObject.ToObject<RootObject>();
    
    jObject["returned_data"]["data"] = JToken.FromObject(newResult);  //<= newResult comes from either one of above linq group by result
    
    string newJson = jObject.ToString();
    
    Console.WriteLine(newJson);
    

    输出:(来自控制台)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 2015-08-11
      相关资源
      最近更新 更多