Lin中的SQL Like功能用于int数据类型 [英] SQL Like functionality in Linq for int datatype

查看:93
本文介绍了Lin中的SQL Like功能用于int数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要在Linq中使用SQL Like功能来获取int字段。这里我使用SqlMethod.Like()作为字符串数据类型,但我不能将它用于int数据类型甚至 - 尽管我在SqlMethod.Like()中使用了字符串转换。这是我试过的示例代码。



示例:



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*";




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





先谢谢



Thanks in Advance

推荐答案

您可以使用Lambda表达式如下所述:



You can use Lambda Expressions for that as mentioned below :

var people = sample.PROCESSes.Where(t => t.ProcessID.ToString().StartsWith("2")).ToList();


how-to-do-a-like-query-with-linq [ ^ ]



希望它会有所帮助..
how-to-do-a-like-query-with-linq[^]

Hope it will help..


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("*", "%")));
      }







这是用于INT DataType的SQL Like Functionality的代码LINQ to ENTITIES



样品

-------




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

Sample
-------

IQueryable<process> processQuery = null;
           if (process.processIDString != null && process.processIDString .Contains("*"))
           {
               processQuery = WildcardSearchByInt<process>("Process_ID", process.processIDString, processEntity);
           }


这篇关于Lin中的SQL Like功能用于int数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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