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;
}