Linq-to-SQL - 不起作用
我在 WCF 服务中有以下代码 C# 代码:
using (DataClasses1DataContext db = new DataClasses1DataContext())
{
var members = from member in db.Stops_edited_smalls.Take(25)
where Math.Abs(Convert.ToDouble(member.Latitude) - curLatitude) < 0.05
&& Math.Abs(Convert.ToDouble(member.Longitude) - curLongitude) < 0.05
select member;
return members.ToList();
}
如上所述
.Take(25)
,不返回任何结果。为什么会这样呢? (没有 .Take(25) 也能正常工作)
I have this code C# code in a WCF service:
using (DataClasses1DataContext db = new DataClasses1DataContext())
{
var members = from member in db.Stops_edited_smalls.Take(25)
where Math.Abs(Convert.ToDouble(member.Latitude) - curLatitude) < 0.05
&& Math.Abs(Convert.ToDouble(member.Longitude) - curLongitude) < 0.05
select member;
return members.ToList();
}
Having the :
.Take(25)
as above, returns no retults. Why so? (it works fine without the .Take(25))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您在对 where 子句进行过滤之前而不是之后获取前 25 个。这 25 条记录均不匹配 where 子句。
如果您只需要前 25 个结果,则应将调用移至 Take():
Because you're taking the first 25 before the filtering on the where clause rather than after. None of those 25 records matches the where clause.
If you only want the first 25 results, you should move the call to Take():
正如 Justin 所说,您的
Take
放在了错误的位置。这就像说“在字典中查找前 25 个单词,然后过滤掉所有不以 B 开头的单词”。这不是查找以 B 开头的前 25 个单词的方式。您可能只想:我更喜欢将
Take
调用单独放到查询表达式中,只是为了可读性。两者都可以工作,并且这确实在数据库中执行“仅前25个值”,而不是在本地。鉴于您只需要“前”25 行,您可能应该指定一个排序,例如
...除非使用您真正想要排序的任何属性。
As Justin says, you've got your
Take
in the wrong place. It's like saying "Find the first 25 words in the dictionary, and then filter out anything that doesn't begin with B." That's not how you find the first 25 words beginning with B. You probably just want:I prefer to put the
Take
call separately to the query expression, just for the sake of readability. Either will work though, and this does perform the "just the first 25 values" in the database, not locally.Given that you only want the "first" 25 rows though, you should probably specify an ordering, e.g.
... except using whatever property you really want to order by.