如何使用 LINQ 在 C# 中重构对象?

发布于 2025-01-09 23:17:05 字数 2000 浏览 1 评论 0 原文

我有一个数据集如下:

[
    {
        "Id": 1,
        "Country": "Uruguay",
        "Name": "Foo",
        "Status": "Completed",
    },
    {
        "Id": 2,
        "Country": "Uruguay",
        "Name": "Foo",
        "Status": "Completed",
    },
    {
        "Id": 3,
        "Country": "Germany",
        "Name": "Foo",
        "Status": "Completed",
    },
]

我想按国家对其进行转换和排序,使其如下所示:

[
    {
        "Country": "Uruguay",
        "Details": [
         {
            "Id": 1,
            "Name": "Foo",
            "Status": "Completed",
         },
         {
            "Id": 2,
            "Name": "Foo",
            "Status": "Completed",
         },
        ],
    },
    {
        "Country": "Germany",
        "Details": [
         {
            "Id": 3,
            "Name": "Foo",
            "Status": "Completed",
         },
        ],
    },
],

这些是 C# 中的类:

public class Countries {
    public int Id { get; set; }
    public string Country { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

public class Details {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

public class CountryList {
    public string Country { get; set; }
    public List<Details> Details { get; set; }
}

我尝试过的一些内容如下所示:

var foo = countries
    .GroupBy(x => new Details { Id = x.Id, Name = x.Name, Status = x.Status })
    .Select( y => new CountryList 
     {
         // Country = y.Key. 
     }

var foo = countries
    .GroupBy(x => x.Country)
    .Select( y => new CountryList 
     {
         // Country = y.Key.
         Details = y.GroupBy(a => new Details 
         { 
            Id = a.Id, 
            Name = a.Name, 
            Status = a.Status  
         }).ToList() 
     }

我无法确定如何使用LINQ 来解决这个问题。我过去做过一些 GroupBy 操作,但我无法解决这个问题。如何将数据集转换为所需的结果?

I have a data set as follows:

[
    {
        "Id": 1,
        "Country": "Uruguay",
        "Name": "Foo",
        "Status": "Completed",
    },
    {
        "Id": 2,
        "Country": "Uruguay",
        "Name": "Foo",
        "Status": "Completed",
    },
    {
        "Id": 3,
        "Country": "Germany",
        "Name": "Foo",
        "Status": "Completed",
    },
]

I want to transform and sort it by Country so that it looks as follows:

[
    {
        "Country": "Uruguay",
        "Details": [
         {
            "Id": 1,
            "Name": "Foo",
            "Status": "Completed",
         },
         {
            "Id": 2,
            "Name": "Foo",
            "Status": "Completed",
         },
        ],
    },
    {
        "Country": "Germany",
        "Details": [
         {
            "Id": 3,
            "Name": "Foo",
            "Status": "Completed",
         },
        ],
    },
],

These are the classes in C#:

public class Countries {
    public int Id { get; set; }
    public string Country { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

public class Details {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

public class CountryList {
    public string Country { get; set; }
    public List<Details> Details { get; set; }
}

Some of what I have tried looks as followed:

var foo = countries
    .GroupBy(x => new Details { Id = x.Id, Name = x.Name, Status = x.Status })
    .Select( y => new CountryList 
     {
         // Country = y.Key. 
     }

var foo = countries
    .GroupBy(x => x.Country)
    .Select( y => new CountryList 
     {
         // Country = y.Key.
         Details = y.GroupBy(a => new Details 
         { 
            Id = a.Id, 
            Name = a.Name, 
            Status = a.Status  
         }).ToList() 
     }

I am having trouble working out how to use LINQ to solve this. I have done a handful of GroupBy operations in the past, but I wasn't able to work this one out. How do I transform my dataset into the desired result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卷耳 2025-01-16 23:17:05

您不需要第二个 GroupBy

var foo = countries
    .GroupBy(x => x.Country)
    .Select(y => new CountryList 
     {
         Country = y.Key,
         Details = y.Select(a => new Details 
         { 
            Id = a.Id, 
            Name = a.Name, 
            Status = a.Status  
         }).ToList() 
     };

You do not need second GroupBy

var foo = countries
    .GroupBy(x => x.Country)
    .Select(y => new CountryList 
     {
         Country = y.Key,
         Details = y.Select(a => new Details 
         { 
            Id = a.Id, 
            Name = a.Name, 
            Status = a.Status  
         }).ToList() 
     };
许你一世情深 2025-01-16 23:17:05

您可以利用.GroupBy() 重载,可让您定义 resultSelector 来创建 CountryList 并填充他们的详细信息

var countries = new List<Countries>
{
    new() { Id = 1, Country = "Uruguay", Name = "Foo", Status = "Completed" },
    new() { Id = 2, Country = "Uruguay", Name = "Foo", Status = "Completed" },
    new() { Id = 3, Country = "Germany", Name = "Foo", Status = "Completed" },
};

List<CountryList> countryList = countries
    .GroupBy(
        c => c.Country,
        ( country, matches ) => new CountryList()
            {
                Country = country,
                Details = matches.Select(match => new Details 
                    { 
                        Id = match.Id, 
                        Name = match.Name, 
                        Status = match.Status 
                    }).ToList()
            })
    .ToList();

(国家/地区,比赛)=> new CountryList() { ... }resultSelector


小提琴示例此处

You can take advantage of the .GroupBy() overload that lets you define a resultSelector to create your CountryLists and populate their Details:

var countries = new List<Countries>
{
    new() { Id = 1, Country = "Uruguay", Name = "Foo", Status = "Completed" },
    new() { Id = 2, Country = "Uruguay", Name = "Foo", Status = "Completed" },
    new() { Id = 3, Country = "Germany", Name = "Foo", Status = "Completed" },
};

List<CountryList> countryList = countries
    .GroupBy(
        c => c.Country,
        ( country, matches ) => new CountryList()
            {
                Country = country,
                Details = matches.Select(match => new Details 
                    { 
                        Id = match.Id, 
                        Name = match.Name, 
                        Status = match.Status 
                    }).ToList()
            })
    .ToList();

, ( country, matches ) => new CountryList() { ... } being the resultSelector.


Example fiddle here.

南…巷孤猫 2025-01-16 23:17:05

试试这个

var orig = JsonConvert.DeserializeObject<List<Countries>>(json); 

List<CountryList> countries = orig.GroupBy(o => o.Country)
.Select(x => new CountryList {
Country = x.Key,
Details = x.Select(o => new Details {Id=o.Id,Name=o.Name,Status=o.Status} ).ToList()
}).ToList();

 

try this

var orig = JsonConvert.DeserializeObject<List<Countries>>(json); 

List<CountryList> countries = orig.GroupBy(o => o.Country)
.Select(x => new CountryList {
Country = x.Key,
Details = x.Select(o => new Details {Id=o.Id,Name=o.Name,Status=o.Status} ).ToList()
}).ToList();

 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文