1

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?

1
  • One way would be to set it to attr.url, then iterate the list after the LINQ doing the necessary replacements? Commented Jul 16, 2012 at 15:21

5 Answers 5

2

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-", "")
Sign up to request clarification or add additional context in comments.

Comments

2

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-", ""));

Comments

1

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.

Comments

1

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;
}

Comments

1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.