gpt4 book ai didi

c# - 我可以使用 LINQ GroupBy 更干净地执行此操作吗?

转载 作者:太空狗 更新时间:2023-10-30 00:37:21 25 4
gpt4 key购买 nike

在这个人为设计的示例中,它非常类似于我的现实问题,我有一个来自外部源的数据集。来自外部源的每条记录都采用以下形式:

[Classification] NVARCHAR(32),
[Rank] INT,
[Data] NVARCHAR(1024)

我希望构建一个对象,其中 RankData 被修补到响应对象的单个实例中,该实例包含三个硬编码 Classification 值,按 Rank 排序。

我有一些有用的东西,但我忍不住认为它可以做得更好。这是我的:

public static void Main()
{
IEnumerable<GroupingTestRecord> records = new List<GroupingTestRecord>
{
new GroupingTestRecord { Classification = "A", Rank = 1, Data = "A1" },
new GroupingTestRecord { Classification = "A", Rank = 2, Data = "A2" },
new GroupingTestRecord { Classification = "A", Rank = 3, Data = "A3" },
new GroupingTestRecord { Classification = "B", Rank = 1, Data = "B1" },
new GroupingTestRecord { Classification = "B", Rank = 2, Data = "B2" },
new GroupingTestRecord { Classification = "B", Rank = 3, Data = "B3" },
new GroupingTestRecord { Classification = "C", Rank = 1, Data = "C1" },
new GroupingTestRecord { Classification = "C", Rank = 2, Data = "C2" },
new GroupingTestRecord { Classification = "C", Rank = 3, Data = "C3" },
};

GroupTestResult r = new GroupTestResult
{
A = records.Where(i => i.Classification == "A").Select(j => new GroupTestResultItem { Rank = j.Rank, Data = j.Data, }).OrderBy(k => k.Rank),
B = records.Where(i => i.Classification == "B").Select(j => new GroupTestResultItem { Rank = j.Rank, Data = j.Data, }).OrderBy(k => k.Rank),
C = records.Where(i => i.Classification == "C").Select(j => new GroupTestResultItem { Rank = j.Rank, Data = j.Data, }).OrderBy(k => k.Rank),
};

源记录DTO:

public class GroupingTestRecord
{
public string Classification { get; set; }
public int? Rank { get; set; }
public string Data { get; set; }
}

目标单类:

public class GroupTestResult
{
public IEnumerable<GroupTestResultItem> A { get; set; }
public IEnumerable<GroupTestResultItem> B { get; set; }
public IEnumerable<GroupTestResultItem> C { get; set; }
}

区别子类:

public class GroupTestResultItem
{
public int? Rank { get; set; }
public string Data { get; set; }
}

输出

{
"A":[
{
"Rank":1,
"Data":"A1"
},
{
"Rank":2,
"Data":"A2"
},
{
"Rank":3,
"Data":"A3"
}
],
"B":[
{
"Rank":1,
"Data":"B1"
},
{
"Rank":2,
"Data":"B2"
},
{
"Rank":3,
"Data":"B3"
}
],
"C":[
{
"Rank":1,
"Data":"C1"
},
{
"Rank":2,
"Data":"C2"
},
{
"Rank":3,
"Data":"C3"
}
]
}

Fiddle

有没有更好的方法来实现我的目标?

最佳答案

相同的 JSON 输出是使用 GroupBy 实现的首先在 Classification并申请 ToDictionary结果 IGrouping<string, GroupingTestRecord>.Key

var r = records
.GroupBy(_ => _.Classification)
.ToDictionary(
k => k.Key,
v => v.Select(j => new GroupTestResultItem { Rank = j.Rank, Data = j.Data, }).OrderBy(k => k.Rank).ToArray()
);

var json = JsonConvert.SerializeObject(r);

Console.WriteLine(json);

应该很容易反序列化到目标单个类(例如在客户端上)

var result = JsonConvert.DeserializeObject<GroupTestResult>(json);

is it possible to get the top level result into a GroupTestResult object?

从字典中构建结果

var result = new GroupTestResult {
A = r.ContainsKey("A") ? r["A"] : Enumerable.Empty<GroupTestResultItem>();,
B = r.ContainsKey("B") ? r["B"] : Enumerable.Empty<GroupTestResultItem>();,
C = r.ContainsKey("C") ? r["C"] : Enumerable.Empty<GroupTestResultItem>();,
};

关于c# - 我可以使用 LINQ GroupBy 更干净地执行此操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52826651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com