Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to filter string list based on some condition as given below,
List<string> config = new List<string>();
               config.Add("userdata.member1");
               config.Add("userdata.member2");
               config.Add("userdata.member3");
               config.Add("userdata.member4");
               config.Add("userdata.member5");
               config.Add("attribute.memberattribute1");
               config.Add("attribute.memberattribute2");
               config.Add("attribute.memberattribute3");
               config.Add("attribute.memberattribute4");
               config.Add("attribute.memberattribute5");

               string[] userDataKeys = config.Select(x => x.Contains("userdata.")?x.Replace("userdata.",""):x).ToArray();


What I have tried:

I want to check whether the string has "userdata." and replace it with empty string finally return the replaced items only.

Expected output:
member1
member2
member3
member4
member5

can anyone tell me how to achieve it using linq.

Thanks.
Posted
Updated 13-Oct-17 3:26am

You're almost there. First you need to identify which records contain userdata.. A where clause helps here:
C#
config.Where(x => x.StartsWith("userdata.")).Select(x => x.Replace("userdata.", string.Empty)).ToList();
 
Share this answer
 
Comments
Maciej Los 13-Oct-17 9:26am    
5ed!
Note: there's no need to use .StartWith or .Contains method. Simple .Replace should be enough. Check my answer.
Pete O'Hanlon 13-Oct-17 10:04am    
If the user was only looking to replace the string, then Replace would be enough; as they also wanted to return the list of records that they changed, the list has to be filtered first - hence the Where clause using StartsWith.
try
string[] userDataKeys = config.Where(x => x.Contains("userdata.")).Select(x => x.Replace("userdata.", "") ).ToArray();
 
Share this answer
 
Comments
Maciej Los 13-Oct-17 9:26am    
5ed!
Note: there's no need to use .StartWith or .Contains method. Simple .Replace should be enough. Check my answer.
Karthik_Mahalingam 13-Oct-17 9:38am    
Thank you Maciej

but the Op's
Expected output is
member1
member2
member3
member4
member5
Maciej Los 13-Oct-17 9:49am    
I know, that's why my second part of answer meets OP's criteria ;)
Karthik_Mahalingam 13-Oct-17 10:38am    
:)counter 5!
Maciej Los 13-Oct-17 12:45pm    
Thank you, Karthik
There's no need to use .StartWith or .Contains method. Simple .Replace should be enough :)

C#
var result = config.Select(x=>x.Replace("userdata.", string.Empty)).ToList();


Of course, if you want to return only data with member word, you can use:
C#
var result = config.Where(x=>x.Contains("member")).Select(x=>x.Replace("userdata.", string.Empty)).ToList();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900