2013-02-11 25 views
0

使用數據庫TABLE名爲Costcenter。 我試圖在CostcenrCode只有三個數字字符的下拉列表中顯示Costcenters。我試圖在ASP.Net WF應用程序的VIEW上執行此操作。現在我已經將它移到了使用LINQ to SQL的DataAccess查詢中。我有點困惑,如何在LINQ查詢中返回String。我在最後選擇cc來填充數據庫表中的所有Costcenter。但我只需要拉一個就好(例如100而不是F.C56)。RegEx與LINQ to SQL查詢過濾要在UI上顯示的數據

我的數據訪問代碼如下:

public static IEnumerable<Costcenter> GetAllCostcentersByCountryCompanyProfitcenterYear(short year, int companyID) 
    { 
     List<Costcenter> costcenters = new List<Costcenter>(); 
     using (var context = new CostReportEntities()) 
     { 
      costcenters = (from cc in context.Costcenters 
          join company in context.Companies on cc.CompanyID equals company.CompanyID 
          where cc.Year == year && cc.CompanyID == companyID 
          select cc).ToList(); 

     } 
     return costcenters; 

    } 

我一直在尋找幾個帖子在這裏,但因爲我在LINQ是新來的SQL不能把任何東西在一起。

+0

您不能將linq的Regex.IsMatch用於SQL。 – 2013-02-11 16:21:18

+0

您使用的是什麼RDMS? SQL Server,Oracle,....? – 2013-02-12 09:09:41

回答

1

這可以達到目的:

var costcenters = (from cc in context.Costcenters 
        join company in context.Companies on cc.CompanyID equals company.CompanyID 
        where cc.Year == year && cc.CompanyID == companyID && 
         SqlMethods.Like(cc.CostcenrCode, "[0-9][0-9][0-9][^0-9]%") 
        select cc).ToList(); 

來源:

LINQ to SQL query to determine if value starts with numeric


編輯1 - 使用SqlMethods.PatIndex加入替代解決方案:

var costcenters = (from cc in context.Costcenters 
        join company in context.Companies on cc.CompanyID equals company.CompanyID 
        where cc.Year == year && cc.CompanyID == companyID && 
         SqlMethods.PatIndex("[0-9][0-9][0-9][^0-9]%", cc.CostcenrCode) > 0 
        select cc).ToList(); 
+0

這似乎不適用於LINQ to Entities,並給出錯誤「LINQ to Entities does not recognized the method'布爾類似於(System.String,System.String)'方法,並且此方法無法轉換爲商店表達式。」 – shaz 2013-02-12 08:59:06

+0

@shaz:我承認我沒有測試它,我認爲它會在Linq中支持Entities。嘗試使用SqlMethods.PatIndex,它可能工作。更新了我的答案。 – 2013-02-12 09:05:47

+0

謝謝。使用System.Data.Objects.SqlClient;和語法SqlFunctions.PatIndex()你的第二個建議工作。雖然它也拉起4位數據。 – shaz 2013-02-12 09:26:07

-1
using System.Text.RegularExpressions; 
    ... 
costcenters = (from cc in context.Costcenters 
        join company in context.Companies on cc.CompanyID equals company.CompanyID 
        where cc.Year == year && cc.CompanyID == companyID && 
         Regex.IsMatch(cc.CostcenrCode, @"^\d{3}$"); 
        select cc).ToList(); 
+1

它顯示一個錯誤「LINQ to Entities does not recognized the method'布爾IsMatch(System.String,System.String)'方法,並且此方法無法轉換爲存儲表達式。」 – shaz 2013-02-11 16:07:12

+0

您不能使用Regex.IsMatch和Linq to SQL或Linq to Entities。它會和Linq一起使用對象,雖然 – 2013-02-11 16:19:55

+0

@RuiJarimba:你會碰巧對上述問題有任何建議嗎? – shaz 2013-02-11 16:25:49

0

您可以使用Regex.Match,但您需要實現您的數據優先。你說,這是下拉列表中的數據,因此該解決方案是可以接受的:

costcenters = (
     from cc in context.Costcenters 
     join company in context.Companies on cc.CompanyID equals company.CompanyID 
     where cc.Year == year && cc.CompanyID == companyID 
     select cc 
    ).AsEnumerable() 
    .Where(cc => Regex.IsMatch(cc.CostcenrCode, @"^\d{3}$")) 
    .ToList(); 

在這裏的技巧是在AsEnumerable()方法,列舉你的數據,你可以申請在後,這個數據正則表達式的方法。

+0

不是最好的解決方案,因爲它會加載數據庫中的所有數據,只有這樣才能應用正則表達式。 – 2013-02-11 16:59:00

+0

@RuiJarimba:是的,我知道。這種解決方案並非針對每種情況,原因。但我認爲,這是一個下拉列表可以接受的。 – Alex 2013-02-11 17:03:10