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, "");
How do this string manipulation with regex into my LINQ query?
According to Guffa and RePierre:
DashboardEntities dashboardDB = new DashboardEntities();
var sites = dashboardDB.Instances
.Select(attr => new SiteModel
{
url = attr.url,
server = attr.server,
pool = attr.pool,
version = attr.version,
client = attr.url
})
.ToList();
sites.ForEach(attr => attr.client = Regex.Replace(attr.client, @"\.[a-z-./]+", "").Replace("formation-", ""));
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.
... .ToList()
.Select(
z => z.client = new Regex(@"\.[a-z-./]+")
.Replace(z.attr.url, "").Replace("formation-", "")
)
Treat 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.
Edit: 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.
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.
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 = attr.url // load the url for further processing
})
.ToList();
// iterate over the list and get client data from the url
sites.ForEach(ite => item.client = GetSubstring(item.client));
return sites;
Where the method GetSubstring
encapsulates the regex processing logic.
private string GetSubstring(string url)
{
String client = "";
Regex rgx = new Regex(@"\.[a-z-./]+");
client = rgx.Replace(attr.url, "");
rgx = new Regex("formation-");
client = rgx.Replace(client, "");
return client;
}
There might be better ways, but what about:
Regex rgx1 = new Regex(@"\.[a-z-./]+");
Regex rgx2 = new Regex("formation-");
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 = rgx2.Replace(rgx1.Replace(attr.url,""),"")
})
.ToList();
return sites;
You don't even need a regular expression for the second replace. You can do it as a single expression with the static overload:
client = Regex.replace(attr.url, @"\.[a-z-./]+", "").Replace("formation-", "")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.