C# 具有字符串操作和正则表达式的LINQ查询结果 上下文
我从数据库中检索站点列表,如下所示:C# 具有字符串操作和正则表达式的LINQ查询结果 上下文,c#,regex,string,linq,entity-framework,C#,Regex,String,Linq,Entity Framework,我从数据库中检索站点列表,如下所示: DashboardEntities dashboardDB = new DashboardEntities(); var sites = dashboardDB.Instances .Select(attr => new SiteModel { server = attr.server, pool = attr.pool, url = attr.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 = ???
})
.ToList();
return sites;
String client = "";
Regex rgx = new Regex(@"\.[a-z-./]+");
client = rgx.Replace(attr.url, "");
rgx = new Regex("formation-");
client = rgx.Replace(client, "");
对于客户端,我需要从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 = ???
})
.ToList();
return sites;
String client = "";
Regex rgx = new Regex(@"\.[a-z-./]+");
client = rgx.Replace(attr.url, "");
rgx = new Regex("formation-");
client = rgx.Replace(client, "");
问题
如何在我的LINQ查询中使用正则表达式进行此字符串操作?您不能以当前形式使用它。对于正则表达式部分,没有已知的SQL转换。但是,一旦调用.ToList,您可以将其添加为后续选择
... .ToList()
.Select(
z => z.client = new Regex(@"\.[a-z-./]+")
.Replace(z.attr.url, "").Replace("formation-", "")
)
将其视为伪代码,但这种通用方法应该能够完成它。然后您只需要在初始选择中设置client=
编辑:作为旁注,组成部分实际上不需要是正则表达式。一个简单的字符串替换就足够了,而且速度更快。您不能以当前的形式替换它。对于正则表达式部分,没有已知的SQL转换。但是,一旦调用.ToList,您可以将其添加为后续选择
... .ToList()
.Select(
z => z.client = new Regex(@"\.[a-z-./]+")
.Replace(z.attr.url, "").Replace("formation-", "")
)
将其视为伪代码,但这种通用方法应该能够完成它。然后您只需要在初始选择中设置client=
编辑:作为旁注,组成部分实际上不需要是正则表达式。一个简单的字符串替换就足够了,而且速度更快。不幸的是,您将无法将正则表达式处理逻辑直接发送到数据库;您需要从数据库中获取url,然后遍历列表以从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;
其中GetSubstring方法封装了正则表达式处理逻辑
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;
}
不幸的是,您将无法将正则表达式处理逻辑直接发送到数据库;您需要从数据库中获取url,然后遍历列表以从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;
其中GetSubstring方法封装了正则表达式处理逻辑
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;
}
也许有更好的方法,但是:
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;
也许有更好的方法,但是:
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;
第二次替换甚至不需要正则表达式。可以使用静态重载将其作为单个表达式执行:
client = Regex.replace(attr.url, @"\.[a-z-./]+", "").Replace("formation-", "")
第二次替换甚至不需要正则表达式。可以使用静态重载将其作为单个表达式执行:
client = Regex.replace(attr.url, @"\.[a-z-./]+", "").Replace("formation-", "")
根据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-", ""));
根据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-", ""));
一种方法是将其设置为attr.url,然后在LINQ执行必要的替换后迭代列表?一种方法是将其设置为attr.url,然后在LINQ执行必要的替换后迭代列表?