实体框架中的查询
本文关键字:查询 框架 实体 | 更新日期: 2023-09-27 17:57:58
我必须使用此查询获取前20行,而like子句不起作用。有人能告诉我为什么/帮我找到解决方案吗?
var zipcodes = (from results in db1.ZipCodes1
where results.CityType == "D" && results.ZIPCode like '%ZC %'
select new Ajaxresults
{
ZIPCode= results.ZIPCode,
CityName = results.CityName,
StateAbbr = results.StateAbbr,
StateName = results.StateName
}).GroupBy(o => o.ZIPCode);
要选择前20行,应使用LINQ Take
运算符
var zipcodes = (from results in db1.ZipCodes1
...
).Take(20);
代码应该是这样的:
var zipcodes = (from results in db1.ZipCodes1
where results.CityType == "D" && results.ZIPCode.Contains("ZC ")
select new Ajaxresults
{
ZIPCode= results.ZIPCode,
CityName = results.CityName,
StateAbbr = results.StateAbbr,
StateName = results.StateName
}).GroupBy(o => o.ZIPCode).Take(20);
String.Contains方法将被转换为相应的SQL LIKE运算符。您可以将查询更改为类似的内容:
var zipcodes = (from results in db1.ZipCodes1
where results.CityType == "D"
&& results.ZIPCode.Contains("ZC ")
select new Ajaxresults
{
ZIPCode= results.ZIPCode,
CityName = results.CityName,
StateAbbr = results.StateAbbr,
StateName = results.StateName
}).GroupBy(o => o.ZIPCode);