+ 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

1st Nov 2022, 1:43 PM
sneeze
sneeze - avatar
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)
1st Nov 2022, 2:11 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 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();
1st Nov 2022, 2:12 PM
Jayakrishna 🇮🇳
+ 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.
2nd Nov 2022, 12:16 PM
sneeze
sneeze - avatar