Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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;
        }
Posted
Updated 3-Nov-16 17:43pm
v3

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.
C#
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;
}
 
Share this answer
 
Comments
hamid-shrk 3-Nov-16 13:44pm    
Thank you my dear friend, it works greatly :)
Possibly a Regex would be simplest to implement here
C#
Regex r = new Regex(number.Replace("*", @"\d"));
....

where r.Match(s.Number).Success
 
Share this answer
 
Comments
Richard Deeming 3-Nov-16 13:16pm    
Good luck getting your database to recognise that! :)
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.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900