语境

我从这样的数据库中检索站点列表:

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;


对于client,我需要从网址中获取一个子字符串,如下所示:

String client = "";

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

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




如何使用正则表达式对我的LINQ查询进行字符串操作?

最佳答案

根据Guffa和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-", ""));

10-08 04:04
查看更多