RegEx与LINQ to SQL查询过滤要在UI上显示的数据

问题描述:

使用数据库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

这可以达到目的:

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

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

您可以使用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