0

I am trying to get a string from a list in c#, but can't find a way to do it. Heres my code

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; }
}

and I want to get, for example, the temp_F string from the current_condition list. How can I do this?

3
  • 2
    You have a class named Data? Really? Commented Mar 4, 2013 at 0:46
  • At least show us some fail tries Commented Mar 4, 2013 at 0:47
  • @MitchWheat yea i should really rename that Commented Mar 4, 2013 at 0:52

4 Answers 4

3

Assuming you want all the temperatures from your list of CurrentCondition instances, you could do this easily using Linq:

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

In light of the accepted answer, here's how to get a specific temperature with Linq:

string temp = current_condition.Where(x => x.observation_time == "08:30").FirstOrDefault(x => x.temp_F);
Sign up to request clarification or add additional context in comments.

1 Comment

The updated answer doesn't actually give what the OP is looking for either, and is not in any way equivalent to the accepted answer.
2

Since current_condition is a list, you would have to know which list index you are interested in. Say you want index 0, you would write

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

Comments

1
List<string> list = new List<string>();
current_condition.ForEach(cond => list.Add(cond.temp_F));

Comments

0

You can use ElementAt(int) to access an object in a list.

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

2 Comments

Or... why not just use the array index? No need to treat the List<T> as if it were an IEnumerable<T>. This solution is O(N), whereas using the array index is O(1).
Yup. You are right. As lots of beginners are confused with treating a list like an array I thought it would be better to post the "long" version. But your code is faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.