簡體   English   中英

帶有字符串操作和正則表達式的LINQ查詢結果

[英]LINQ query result with string manipulation and regex

語境

我從這樣的數據庫中檢索站點列表:

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 ,我需要從url這樣獲得一個子字符串:

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

您無法以當前的形式擁有它。 正則表達式部分將沒有已知的SQL轉換。 但是,一旦調用.ToList() ,就可以將其添加為后續選擇。

...   .ToList()
      .Select(
          z => z.client = new Regex(@"\.[a-z-./]+")
               .Replace(z.attr.url, "").Replace("formation-", "")
      )

將其視為偽代碼,但該通用方法應該能夠完成它。 然后,您只需要在初始選擇中設置client = ""

編輯:作為旁注,“ formation-”部分確實不需要是Regex。 一個簡單的字符串替換就足夠了,並且會更快。

不幸的是,您將無法直接將正則表達式處理邏輯發送到數據庫。 您需要從數據庫獲取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;

您甚至不需要第二次替換的正則表達式。 您可以將其作為單個表達式進行靜態重載:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM