gpt4 book ai didi

c# - 从名称匹配的列表列中选择值

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:14 25 4
gpt4 key购买 nike

我有一个包含不同列NameBitrate的列表。

public class VideoQuality
{
public string Name { get; set; }
public string Bitrate { get; set; }
}

public List<VideoQuality> quality = new List<VideoQuality>()
{
new VideoQuality() { Name = "High", Bitrate = "5000K" },
new VideoQuality() { Name = "Medium", Bitrate = "2500K" },
new VideoQuality() { Name = "Low", Bitrate = "500K" },
};




如何从 BitrateHigh的列表中选择?

就像是:

if (selectedQuality == "High")
{
// Select High Bitrate from List
string vBitrate = quality.Select(x => x.Bitrate)
.Where(Name = "High");
}


vBitrate应等于 5000K

最佳答案

您首先要过滤,然后选择:

var result = quality.Where(x => x.Name == "High")
.Select(x => x.Bitrate); //IEnumerable<string>


在查询语法中:

var result = from x in quality
where x.Name == "High"
select x.Bitrate; //IEnumerable<string>


请注意,无论如何,结果都是 IEnumerable<string>而不是单个项目。如果只需要一个项目,请使用 FirstOrDefault / First / SingleSingleOrDefault

var result = quality.FirstOrDefault(x => x.Name == "High")?.Bitrate; //string


有关不同方法之间的差异,请参见文档和 LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria

关于c# - 从名称匹配的列表列中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51377432/

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