如何在C#linq中逐字符检查字符串 [英] How to check a string character by character in C# linq

查看:100
本文介绍了如何在C#linq中逐字符检查字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我的数据库表中有一个字段为SimCardNumber,

现在,我有一个用于搜索SimCard Number的表单,用户可以输入数字在特定的位置。

例如他们将搜索9 * 2 *** 54 **

我想在我的桌子上显示任何SimCardNumber在这种模式中。

我需要查询它。

感谢您的帮助



我尝试过:



Hi Friends,
I have a field in my database table as "SimCardNumber",
Now, I have a form for search SimCard Number and users can put numbers in the specific position.
for example they will search for 9*2***54**
and i want to show whatever "SimCardNumber" from my table which is in this pattern.
I need a query for it.
thank's for your help

What I have tried:

public IQueryable<SimCardOutPut> GetSimCardBySearch(string number)
        {
            var context = new Entities();
            var simcard = context.SimCards;
            var operators = context.Operators;
            var rondtype = context.RondTypes;
            var city = context.Cities;
            var query = from s in simcard
                        join o in operators on s.IdOperatorFK equals o.IdOperator
                        join r in rondtype on s.IdRondTypeFK equals r.IdRondType
                        join c in city on s.IdCityFK equals c.IdCity
                        where
                            s.Number.Contains(number)
                        select new SimCardOutPut()
                        {
                            IdSimCard = s.IdSimCard,
                            Operator = o.Title,
                            RondType = r.Title,
                            City = c.Title,
                            Number = s.Number,
                            Type = s.Type,
                            Status = s.Status,
                            Quality = s.Quality,
                            Seller = s.Seller,
                            Price = (Int64)s.Price,
                            ShowType = s.ShowType,
                            Active = (Boolean)s.Active
                        };
            return query;
        }

推荐答案

不幸的是,实体中没有 LIKE 函数框架,使用Code First,似乎没有选择创建一个。



但是,如果你添加对 EntityFramework.SqlServer 程序集,你可以使用 SqlFunctions.PatIndex [ ^ ]方法。不幸的是,它不会像 LIKE 查询一样快,但它应该比加载所有数据和过滤客户端更快。

Unfortunately, there's no LIKE function in Entity Framework, and with Code First, there doesn't seem to be an option to create one.

However, if you add a reference to the EntityFramework.SqlServer assembly, you can use the SqlFunctions.PatIndex[^] method. Unfortunately, it won't be as quick as a LIKE query, but it should be faster than loading all of the data and filtering client-side.
public IQueryable<SimCardOutPut> GetSimCardBySearch(string number)
{
    string sqlPattern = number.Replace('*', '_');
    
    var context = new Entities();
    var simcard = context.SimCards;
    var operators = context.Operators;
    var rondtype = context.RondTypes;
    var city = context.Cities;
    var query = from s in simcard
                join o in operators on s.IdOperatorFK equals o.IdOperator
                join r in rondtype on s.IdRondTypeFK equals r.IdRondType
                join c in city on s.IdCityFK equals c.IdCity
                where
                    SqlFunctions.PatIndex(sqlPattern, s.Number) != 0
                select new SimCardOutPut()
                {
                    IdSimCard = s.IdSimCard,
                    Operator = o.Title,
                    RondType = r.Title,
                    City = c.Title,
                    Number = s.Number,
                    Type = s.Type,
                    Status = s.Status,
                    Quality = s.Quality,
                    Seller = s.Seller,
                    Price = (Int64)s.Price,
                    ShowType = s.ShowType,
                    Active = (Boolean)s.Active
                };
    return query;
}


这里可能最容易实现一个正则表达式

Possibly a Regex would be simplest to implement here
Regex r = new Regex(number.Replace("*", @"\d"));
....

where r.Match(s.Number).Success


只是露水有用的链接RegEx



以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

.NET Regex Tester - Regex Storm [ ^ ]

Expresso正则表达式工具 [ ^ ]

这个显示RegEx是一个不错的图表了解RegEx的作用非常有用:

Debuggex:Online视觉正则表达式测试仪。 JavaScript,Python和PCRE。 [ ^ ]
Just a dew useful links for RegEx

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


这篇关于如何在C#linq中逐字符检查字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆