Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to use the SQL Like functionality in Linq for int field. Here i used SqlMethod.Like() for string datatype but i cant use this for int datatype even-though i used the string conversion inside the SqlMethod.Like(). Here is sample code which i tried .

Sample:

string check = "2*";

SQL
var people = (from t in sample.PROCESSes
                          where SqlMethods.Like(SqlFunctions.StringConvert((double)t.ProcessID), (check.Replace("*", "%"))) == true
                          select t).ToList();


Thanks in Advance
Posted
Updated 26-Aug-13 17:59pm
v2

You can use Lambda Expressions for that as mentioned below :

C#
var people = sample.PROCESSes.Where(t => t.ProcessID.ToString().StartsWith("2")).ToList();
 
Share this answer
 
Comments
RajeshKumar Sugumar 22-Aug-13 4:42am    
string check = "2*" or "*2" or "2*2"
like this i want to filter the ProcessID. So i Cant use Lamda Expressions
CodeBlack 22-Aug-13 4:49am    
can you post your sql query sample ?
RajeshKumar Sugumar 22-Aug-13 5:04am    
This is Query i am using in SqlServer.

Select * from PROCESS Where CAST(ProcessID as varchar) Like '2*'

The Linq Query which i tried is

var people = (from t in sample.PROCESSes
where SqlMethods.Like(SqlFunctions.StringConvert((double)t.ProcessID), (check.Replace("*", "%"))) == true
select t).ToList();

AND

var query1 = from c in sample.PROCESSes
where SqlFunctions.CharIndex(SqlFunctions.StringConvert((double)c.ProcessID), check) > 0
select c;
these are ways but i not getting the output ......
CodeBlack 22-Aug-13 5:11am    
Select * from PROCESS Where CAST(ProcessID as varchar) Like '2*'

is it '2*' or '2%'
RajeshKumar Sugumar 22-Aug-13 5:14am    
its "2%" only
how-to-do-a-like-query-with-linq[^]

Hope it will help..
 
Share this answer
 
SQL
public IQueryable<TRow> WildcardSearchByInt<TRow>(string propertyName, string searchCritriea,IObjectSet<TRow> records) where TRow :class
      {
          return ((ObjectSet<TRow>)records).Where("CAST(it." + propertyName + " as System.String) LIKE @search", new ObjectParameter("search", searchCritriea.Replace("*", "%")));
      }



This is code which is used for SQL Like Functionality for INT DataType in LINQ to ENTITIES

Sample
-------
XML
IQueryable<process> processQuery = null;
           if (process.processIDString != null && process.processIDString .Contains("*"))
           {
               processQuery = WildcardSearchByInt<process>("Process_ID", process.processIDString, processEntity);
           }
 
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