C#从列表中获取字符串

C#从列表中获取字符串,c#,string,list,C#,String,List,我试图从c#中的列表中获取字符串,但找不到方法。这是我的密码 public class CurrentCondition { public string cloudcover { get; set; } public string humidity { get; set; } public string observation_time { get; set; } public string precipMM { get; set; } public st

我试图从c#中的列表中获取字符串,但找不到方法。这是我的密码

public class CurrentCondition
{
    public string cloudcover { get; set; }
    public string humidity { get; set; }
    public string observation_time { get; set; }
    public string precipMM { get; set; }
    public string pressure { get; set; }
    public string temp_C { get; set; }
    public string temp_F { get; set; }
    public string visibility { get; set; }
    public string weatherCode { get; set; }
    public List<WeatherDesc> weatherDesc { get; set; }
    public List<WeatherIconUrl> weatherIconUrl { get; set; }
    public string winddir16Point { get; set; }
    public string winddirDegree { get; set; }
    public string windspeedKmph { get; set; }
    public string windspeedMiles { get; set; }
}
    public class Data
{
    public List<CurrentCondition> current_condition { get; set; }
}
公共类当前条件
{
公共字符串cloudcover{get;set;}
公共字符串{get;set;}
公共字符串观察时间{get;set;}
公共字符串precipMM{get;set;}
公用管柱压力{get;set;}
公共字符串temp_C{get;set;}
公共字符串temp_F{get;set;}
公共字符串可见性{get;set;}
公共字符串天气代码{get;set;}
公共列表weatherDesc{get;set;}
公共列表weatherIconUrl{get;set;}
公共字符串winddir16Point{get;set;}
公共字符串winddirDegree{get;set;}
公共字符串windspeedKmph{get;set;}
公共字符串windspeedMiles{get;set;}
}
公共类数据
{
公共列表当前_条件{get;set;}
}

例如,我想从
current\u condition
列表中获取
temp\u F
字符串。我该怎么做呢?

因为
当前条件是一个列表,所以您必须知道您感兴趣的列表索引。假设你想要索引0,你会写

Data data = new Data();
// Some code that populates `current_condition` must somehow run
string result = data.current_condition[0].temp_F.

由于当前条件是一个列表,您必须知道您感兴趣的列表索引。假设你想要索引0,你会写

Data data = new Data();
// Some code that populates `current_condition` must somehow run
string result = data.current_condition[0].temp_F.

假设您需要CurrentCondition实例列表中的所有温度,您可以使用Linq轻松实现这一点:

List<string> temps = current_condition.Select(x => x.temp_F).ToList();

假设您需要CurrentCondition实例列表中的所有温度,您可以使用Linq轻松实现这一点:

List<string> temps = current_condition.Select(x => x.temp_F).ToList();
List List=新列表();
当前条件ForEach(cond=>list.Add(cond.temp_F));
列表=新列表();
当前条件ForEach(cond=>list.Add(cond.temp_F));
您可以使用ElementAt(int)访问列表中的对象

String t = current_condition.ElementAt(index).temp_F;
可以使用ElementAt(int)访问列表中的对象

String t = current_condition.ElementAt(index).temp_F;

您有一个名为Data的类?真的吗?至少给我们看一些失败的例子tries@MitchWheat是的,我真的应该重命名你有一个名为Data的类?真的吗?至少给我们看一些失败的例子tries@MitchWheat是的,我真的应该重新命名,更新后的答案实际上也没有给出OP想要什么,并且在任何方面都不等同于接受的答案。更新后的答案也没有给出OP想要什么,在任何方面都不等同于公认的答案。或者。。。为什么不直接使用数组索引呢?无需将
列表
视为一个
IEnumerable
。这个解是O(N),而使用数组索引是O(1)。是的。你是对的。由于很多初学者对将列表当作数组感到困惑,我认为最好发布“长”版本。但是你的代码更快。或者。。。为什么不直接使用数组索引呢?无需将
列表
视为一个
IEnumerable
。这个解是O(N),而使用数组索引是O(1)。是的。你是对的。由于很多初学者对将列表当作数组感到困惑,我认为最好发布“长”版本。但是你的代码更快。