Linq select strings from list when condition is met and save the index
Solution 1
list.Select((item, index) => new { item, index })
.Where(o => o.item.Contains(':'))
Solution 2
not sure what you want as the result ? a list of strings? or ?
but anyways.....with the index prefixed to your string...
List<string> lstOne = new List<string>() { "January:1", "February", "March:4" };
var list = lstOne.Select((s, i) => i+ " " + s ).Where(s => s.Contains(":")).ToList();
ademg
My passion for technology comes from childhood. I grew up playing with console computers but always wanted to know how the game was made and who made it. My first experience with programming languages was Quick Basic. I experimented with Perl and PHP to finally end up as a #dotnet #developer in love with #ruby and #java #Devops enthusiast with over hundred successful projects. I do have a bunch of Microsoft Certificates and might be obsessive-compulsive for clean code.
Updated on June 04, 2022Comments
-
ademg almost 2 years
I have a list of strings
List<string> lstOne = new List<string>() { "January:1", "February", "March:4"};
And I am filtering the strings that contain : with this code
var withcolumns = lstOne.Find(t => t.Contains(':'));
and I am getting a new list with
{ "January:1", "March:4"}
I want to select in a new list the values
January:1
andMarch:4
but also save the indexes of then in the previous list so the result would be"0" "January:1"
"2" "March:4"I can be simple or complicated but right now my brain is not functioning to solve this issue.