gpt4 book ai didi

c# - LINQ 过滤器 LIST 值

转载 作者:行者123 更新时间:2023-11-30 13:43:27 25 4
gpt4 key购买 nike

我有 LINQ 查询

Label = c.Name.Translations.Select(label => new Label
{
Rus = label.Text,
Eng = label.Text,
}),

翻译类

public int Id { get; set; }

public string Text { get; set; }

public virtual ICollection<Translation> Translations { get; set; }



public class Translation
{
public int Id { get; set; }
public string Language { get; set; }
public string Text { get; set; }
}

像这样返回列表

{
"rus":"Нью-Йорк",
"eng":"Нью-Йорк"
},
{
"rus":"New-York",
"eng":"New-York"

我的目标是拥有一件这样的元素

"rus":"Нью-Йорк",
"eng":"New-York"

如何过滤?

最佳答案

这应该可以完成工作:

var labels = new Dictionary<string, string>();
foreach(var item in c.Name.Translations)
{
labels.add(item.Language, item.Text);
}

编辑

var labels = c.Name.Translations.ToDictionary(t => t.Language, t => t.Text);

关于c# - LINQ 过滤器 LIST 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575250/

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