Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code which extends my filter functionality to allow me to search for more than one Zip-code. Ex. (1254,125,145,1455)



C#
if (Post_Code_PAR != null)
{
    String[] PostCode_Array = Post_Code_PAR.Split(',');
    query = from p in query where PostCode_Array.Contains(p.Post_Code) select p;
}



I want to extend this code in a way that if I give it something like (0*) it will find zip codes which starts with 0. If I give it 123* it will give me all zip codes which starts with 123. Thanks a lot.

also tried this:

C#
String[] PostCode_Array = from p in Post_Code_PAR.Split(',') select String.Format("^{0}$", p.Replace("", @"\d"));
query = from p in query where PostCode_Array.Any(pattern => Regex.IsMatch(p.Post_Code, pattern)) select p;


But Iam getting this error:
Error 7 The type 'System.Collections.Generic.IEnumerable <string> "can not be implicitly converted to' string [] '. There is already an explicit conversion exists. (Maybe you missing a cast.)
Posted
Updated 15-Apr-15 4:07am
v5
Comments
Anil_Kumar_India 15-Apr-15 9:55am    
Use Regular Expression, refer https://msdn.microsoft.com/en-us/library/hs600312(v=vs.110).aspx

1 solution

What about:
C#
String[] PostCode_Array = (from p in Post_Code_PAR.Split(',')
                          select String.Format("^{0}$", p.Replace("", @"\d")))
                          .ToArray();

?
 
Share this answer
 
v2
Comments
Member 11609637 16-Apr-15 5:59am    
Hi, Even when I change the Post_code_Array as you said I get a red line Reg.
Member 11609637 16-Apr-15 7:14am    
I have this code but the results are not 100% Correct:
char[] delimiters = {'*'}; // never know what a user might try to use
if (Post_Code_PAR.Contains('*') & delimiters.Any(a => Post_Code_PAR.Contains(a)))
{
string[] codes = Post_Code_PAR.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
query = query.Where(a => codes.Any(b => a.Post_Code.StartsWith(b)));
}
else
{
//query = query.Where(a => a.Post_Code.StartsWith(Post_Code_PAR));
String[] PostCode_Array = Post_Code_PAR.Split(',');
query = from p in query where PostCode_Array.Contains(p.Post_Code) select p;
}

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