Home:ALL Converter>LINQ query result with string manipulation and regex

LINQ query result with string manipulation and regex

Ask Time:2012-07-16T23:14:11         Author:GG.

Json Formatter

Context

I retrieve a list of sites from a database like this:

DashboardEntities dashboardDB = new DashboardEntities();

var sites = dashboardDB.Instances
    .Select(attr => new SiteModel
        {
            server = attr.server,
            pool = attr.pool,
            url = attr.url,
            version = attr.version,
            client = ???
        })
    .ToList();

return sites;

For client, I need to get a substring from the url like this:

String client = "";

Regex rgx = new Regex(@"\.[a-z-./]+");
client = rgx.Replace(attr.url, "");

rgx = new Regex("formation-");
client = rgx.Replace(client, "");

Question

How do this string manipulation with regex into my LINQ query?

Author:GG.,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11507239/linq-query-result-with-string-manipulation-and-regex
Joel Etherton :

You can't in the current form you have it. There will be no known translation to SQL for the regex portion. However, you could add it on as a subsequent select once the .ToList() is called.\n\n... .ToList()\n .Select(\n z => z.client = new Regex(@\"\\.[a-z-./]+\")\n .Replace(z.attr.url, \"\").Replace(\"formation-\", \"\")\n )\n\n\nTreat that as pseudo code, but that general approach should be able to get it done. Then you'd just need to set client = \"\" in the initial select.\n\nEdit: As a side note, the \"formation-\" piece really doesn't need to be a Regex. A simple string replace should suffice there and would be faster.",
2012-07-16T15:24:47
RePierre :

Unfortunatelly, you won't be able to send the regex processing logic directly to the database; you'll need to get the url from the database and then iterate over the list to get client data from the url.\n\nDashboardEntities dashboardDB = new DashboardEntities(); \nvar sites = dashboardDB.Instances \n .Select(attr => new SiteModel \n { \n server = attr.server, \n pool = attr.pool, \n url = attr.url, \n version = attr.version, \n client = attr.url // load the url for further processing\n }) \n .ToList();\n// iterate over the list and get client data from the url\nsites.ForEach(ite => item.client = GetSubstring(item.client)); \nreturn sites; \n\n\nWhere the method GetSubstring encapsulates the regex processing logic.\n\nprivate string GetSubstring(string url)\n{\n String client = \"\"; \n Regex rgx = new Regex(@\"\\.[a-z-./]+\"); \n client = rgx.Replace(attr.url, \"\"); \n rgx = new Regex(\"formation-\"); \n client = rgx.Replace(client, \"\"); \n return client;\n}\n",
2012-07-16T15:25:36
Guffa :

You don't even need a regular expression for the second replace. You can do it as a single expression with the static overload:\n\nclient = Regex.replace(attr.url, @\"\\.[a-z-./]+\", \"\").Replace(\"formation-\", \"\")\n",
2012-07-16T15:27:34
GG. :

According to Guffa and RePierre:\n\nDashboardEntities dashboardDB = new DashboardEntities();\n\nvar sites = dashboardDB.Instances\n .Select(attr => new SiteModel\n {\n url = attr.url,\n server = attr.server,\n pool = attr.pool,\n version = attr.version,\n client = attr.url\n })\n .ToList();\n\nsites.ForEach(attr => attr.client = Regex.Replace(attr.client, @\"\\.[a-z-./]+\", \"\").Replace(\"formation-\", \"\"));\n",
2012-07-17T12:03:19
yy