0

Hey all I am trying to query for the string number thats in the first position inside my List:

List<string[]> idMainDescriptionIcon = new List<string[]>(){
   //              [ID]       [Main]               [Description]               "XX[d=day or n=night]"
   new string[4] { "200", "Thunderstorm",  "thunderstorm with light rain",     "11" },
   new string[4] { "201", "Thunderstorm",  "thunderstorm with rain",           "11" },
   new string[4] { "202", "Thunderstorm",  "thunderstorm with heavy rain",     "11" },
   new string[4] { "210", "Thunderstorm",  "light thunderstorm",               "11" },
   new string[4] { "211", "Thunderstorm",  "thunderstorm",                     "11" }
};

And the Linq I am using:

List<string> d = idMainDescriptionIcon[0][0]
  .Where(x => x.StartsWith("202"))
  .Select(x => x)
  .ToList();

I am getting an error on the idMainDescriptionIcon[0][0] stating:

Error CS1061 'char' does not contain a definition for 'StartsWith' and no accessible extension method 'StartsWith' accepting a first argument of type 'char' could be found (are you missing a using directive or an assembly reference?)

The D should have the values of "202", "Thunderstorm", "thunderstorm with heavy rain", "11".

And this is where I am stuck at. Not sure how to go about fixing this error?

UPDATE #1

When removing the [0][0] and replacing it with just one [0] this is the return I get:

enter image description here

2
  • idMainDescriptionIcon[0][0] got one too many [0]. Commented Oct 26, 2021 at 2:12
  • Thanks for the reply @Jawad. I have updated my OP to show what I get when only using the one [0] instead of [0][0]. Commented Oct 26, 2021 at 2:13

1 Answer 1

1

The problem here is idMainDescriptionIcon[0][0], which is referring to a single string here. Iterating over it would be iterating over characters in the string, which is why you get the error 'char' does not contain a definition for 'StartsWith'

What you would need is the following

var d = idMainDescriptionIcon
  .Where(x => x[0].StartsWith("202"))
  .SelectMany(x => x)
  .ToList();

You need to query the entire idMainDescriptionIcon such that the first element of the inner array starts with "202".

Alternatively,

var d = idMainDescriptionIcon
  .FirstOrDefault(x => x[0].StartsWith("202"))
  .ToList();

Output

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

That did the trick Anu! It works as it should now :)
You may also have been right about SelectMany(), where the op wants a collection of strings directly, though that is a slightly unusual way to request the data.
@JoelCoehoorn Any particular reason to prefer x[0] over First() in your Edit?
The combination of StartsWith() in the Where() along with SelectMany() could result in unexpected output depending on what @StealthRT is attempting. Replacing Where() with FirstOrDefault() might be better.
@Corey Done, Thanks for the suggestion

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.