如何使用linq多列表条件?

时间:2019-03-31 12:50:41

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

我想根据条件通过使用其他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
                    }
                ]
            }
        ]
    }
}

但是我想获得新结果,按“名字”对每个列表进行新排序

{
    "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.

请帮助我。谢谢。

2 个答案:

答案 0 :(得分:1)

您需要使用正确的分组依据,然后从结果分组中仅选择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);

输出:(从控制台)

enter image description here

答案 1 :(得分: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
          }
        ]
      }
    ]
  }
}