+ 3
How to find a line in a text using LINQ
I am trying to filter one line in a text that meets the search criteria. string item = items.Where(i => i.Contains("lemon")).ToList(); I am using LINQ, and do get the following error error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'string' Why do I get this error and how can I solve this ? https://code.sololearn.com/c35n2pEOjUPX/?ref=app
3 odpowiedzi
+ 3
Your query returns a list of string, you won't be able to assign it to a string.
Try using LINQ's First method instead:
string item = items.First(i => i.Contains("lemon"));
I hope this helps :)
(oops I misread the question, I thought you needed one line that matched)
+ 6
You cant assign list to string..
So string item = .. for list Is error.
Use list instead
List<string> item = items.Where(i => i.Contains("lemon")).ToList();
+ 1
Yes, thank you. I do understand it.
I changed my code.
With First I can get the first occurance
With Where and String.Join, I can get all occurances if I want to and make a concatenated string.