Linq-to-SQL - 不起作用

发布于 2024-12-09 21:50:49 字数 600 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捶死心动 2024-12-16 21:50:49

因为您在对 where 子句进行过滤之前而不是之后获取前 25 个。这 25 条记录均不匹配 where 子句。

如果您只需要前 25 个结果,则应将调用移至 Take():

var members = (from member in db.Stops_edited_smalls
              where Math.Abs(Convert.ToDouble(member.Latitude)
                  - curLatitutde) < 0.05
              && Math.Abs(Convert.ToDouble(member.Longitude)
                  - curLongitude) < 0.05
              select member).Take(25);

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():

var members = (from member in db.Stops_edited_smalls
              where Math.Abs(Convert.ToDouble(member.Latitude)
                  - curLatitutde) < 0.05
              && Math.Abs(Convert.ToDouble(member.Longitude)
                  - curLongitude) < 0.05
              select member).Take(25);
变身佩奇 2024-12-16 21:50:49

正如 Justin 所说,您的 Take 放在了错误的位置。这就像说“在字典中查找前 25 个单词,然后过滤掉所有不以 B 开头的单词”。这不是查找以 B 开头的前 25 个单词的方式。您可能只想:

using (DataClasses1DataContext db = new DataClasses1DataContext())
{
    var members = 
      from member in db.Stops_edited_smalls
      where Math.Abs(Convert.ToDouble(member.Latitude) - curLatitude) < 0.05
         && Math.Abs(Convert.ToDouble(member.Longitude) - curLongitude) < 0.05
      select member;

    return members.Take(25).ToList();
}

我更喜欢将 Take 调用单独放到查询表达式中,只是为了可读性。两者都可以工作,并且这确实在数据库中执行“仅前25个值”,而不是在本地。

鉴于您只需要“前”25 行,您可能应该指定一个排序,例如

    var members = 
      from member in db.Stops_edited_smalls
      where Math.Abs(Convert.ToDouble(member.Latitude) - curLatitude) < 0.05
         && Math.Abs(Convert.ToDouble(member.Longitude) - curLongitude) < 0.05
      orderby member.Latitude
      select member;

...除非使用您真正想要排序的任何属性。

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:

using (DataClasses1DataContext db = new DataClasses1DataContext())
{
    var members = 
      from member in db.Stops_edited_smalls
      where Math.Abs(Convert.ToDouble(member.Latitude) - curLatitude) < 0.05
         && Math.Abs(Convert.ToDouble(member.Longitude) - curLongitude) < 0.05
      select member;

    return members.Take(25).ToList();
}

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.

    var members = 
      from member in db.Stops_edited_smalls
      where Math.Abs(Convert.ToDouble(member.Latitude) - curLatitude) < 0.05
         && Math.Abs(Convert.ToDouble(member.Longitude) - curLongitude) < 0.05
      orderby member.Latitude
      select member;

... except using whatever property you really want to order by.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文