2

I have a dataset that is returned from a stored procedure that I save into a string LIST.

So far this is what I have and it's working correctly:

 this.UserOrgs = ds.Tables[0].AsEnumerable()
           .Select(r => r.Field<string>(0))
                            .ToList();

However, I also need to replace all "X" with "Y" in the returned values before I put them into the list.

I guess my question is, can I do it all at one time using LINQ? Or should I replace the characters in the dataset before I convert it into a list?

2 Answers 2

6

Sure, it's easy to do this - you don't even need another call:

this.UserOrgs = ds.Tables[0].AsEnumerable()
                            .Select(r => r.Field<string>(0).Replace("X", "Y"))
                            .ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

puuurfect, thanks! can i trim the YYYYY that i now have at the end. the resulting (2345-YYYYY, i want to display as: 2345-Y, if i use .TrimEnd("Y") it takes out ALL "Y"s and i just have 2345-
0
var chars = new[] { "X", "Y" };
EnumerableRowCollection<string> enumerableRowCollection = dataTable.AsEnumerable().Select(r => chars.Aggregate(r.Field<string>(0), (s, s1) => s.Replace(s1, string.Empty)));

The above answer will replace all X's and all Y's.

At least anything that you put in the char array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.