Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public ActionResult GetRegion(string countryCode)
        {
            if (!string.IsNullOrWhiteSpace(countryCode))
            {
          List<Lsite> Lsites = db.Site.Where(t => t.Nom.Substring(1,5) == countryCode)
                                     .Select(x => new Lsite
                                     {
                                         CountryCode = x.code,
                                         Name = x.Nom
                                     }).ToList();
                return Json(Lsites, JsonRequestBehavior.AllowGet);
            }
            return null;
        }


What I have tried:

I want to know the good syntax.
This one in not correct
Posted
Updated 19-Dec-19 6:49am
Comments
Richard Deeming 19-Dec-19 11:39am    
You're going to need to explain why this one is "not correct". Remember, we can't see your data, and we don't know what you're expecting this code to do.

NB: One obvious problem would be if a site's name didn't have at least six characters. Or perhaps the country code starts at the first character, so you should be using Substring(0, 5)?
Member 14663996 19-Dec-19 12:45pm    
Substring(0, 5) he is the good syntax. thank
Richard Deeming 19-Dec-19 12:46pm    
In that case, it would probably be better to use StartsWith. That way, you won't get an exception if the name is too short.
ZurdoDev 19-Dec-19 12:28pm    
Reply to the comment so that the person who made the comment is notified.

1 solution

As discussed in the comments, either use Substring(0, 5):
C#
db.Site.Where(t => t.Nom.Substring(0, 5) == countryCode)
or StartsWith:
C#
db.Site.Where(t => t.Nom.StartsWith(countryCode))
Substring will throw an exception if the name is not at least five characters long.

StartsWith can return true even if the countryCode is not exactly five characters long.
 
Share this answer
 

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