使用Lambda的EF中的SQL表达式

时间:2013-06-14 12:02:18

标签: c# asp.net linq entity-framework lambda

我想知道类似于this(linq to SQL)

的内容
customers.Where(c => SqlMethods.Like(c.Name, "%john%"));

可以在Entity Framework中完成。最好使用lamba表达式。

我的目标是做这样的事情:

string searchString1 = "%foo";
string searchString2 = "%foo%";
string searchString3 = "foo";

customers.Where(c => SqlMethods.Like(c.Name, searchStringX));

4 个答案:

答案 0 :(得分:0)

Custumers.where(C => c.name.contains( “foo” 的))

你可以试试这个100%的工作

答案 1 :(得分:0)

不知道lambda表达式中的任何新内容,但您可以编写这样的linq查询:

var result = from i in customers 
         where i.name.Contains("yourString") 
         select i;

该列的数据类型也应为varchar

更新:刚用lambda表达式

计算出来
var result = customers.Where(c => c.name.StartsWith("yourString"));
var result = customers.Where(c => c.name.EndsWith("yourString"));
var result = customers.Where(c => c.name.Contains("yourString"));

答案 2 :(得分:0)

您可以使用字符串contains,starts with和end结束方法来执行相同的操作:

string searchString1 = "foo"

var customerList = from x in customers where x.Name.Contains(searchString1) select x;

 var customerList = from x in customers where x.Name.StartsWith(searchString1) select x;

 var customerList = from x in customers where x.Name.EndsWith(searchString1) select x;

答案 3 :(得分:0)

我最终得到了以下解决方案:

.Where(c => SqlFunctions.PatIndex("%John", c.Name) > 0);