2016-07-27 244 views
0

中的任何字母我正在寻找具有FirstName = "abcxyz"成功地与下面Linq查询表中的记录。LINQ的 - 搜索表记录中包含搜索字符串

db.People.Where(p => searchString.Contains(p.FirstName).ToList(); 

但我想搜索在FirstName = "abcxyz"

包含任何字母的表中的记录就像我们在SQL做 -

enter image description here

任何建议将是有益的在这里。

回答

4

望着SQL,你需要的是:

p.FirstName.Contains(searchString) 

所以你的查询是:

db.People.Where(p => p.FirstName.Contains(searchString)).ToList(); 
+2

缺少相同括号的OP是,但你有正确的想法。 :) – itsme86

+0

@ itsme86,谢谢 – Habib

+0

@ itsme86,谢谢!它工作perfectaly :) –

1

可以在LINQ使用下面的方法,要使用像SQL运营商获取数据

例子:

1)如果你想获得的数据开始与一些信我们使用

在SQL: -

select * from People where firstname LIKE '%abc'; 

在LINQ: -

db.People.Where(p => p.firstname.StartsWith(abc)); 

2)如果你想获得的数据包含了我们正在使用

任何字母在SQL

select * from people where firstname LIKE '%abc%'; 

在LINQ

db.people.where(p => p.Contains(abc)); 

3)如果你想获得一些字母结尾的数据,我们使用

在SQL

select * from people where firstname LIKE '%abc'; 

在LINQ

db.people.where(p => p.firstname.EndsWith(abc)); 
相关问题