“不能在 DateTime 上调用方法",以及其他限制 [英] "Cannot call methods on DateTime", and other limitations

查看:24
本文介绍了“不能在 DateTime 上调用方法",以及其他限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道在编译时未捕获的 LINQ to SQL 查询限制的明确列表,以及(在可能的情况下)限制的解决方法?

Does anyone know of a definitive list of LINQ to SQL query limitations that are not trapped at compile time, along with (where possible) workarounds for the limitations?

到目前为止,我们拥有的列表是:

The list we have so far is:

  • DateTime上调用.Date等方法
    • 未找到解决方法
    • 简单,用== ""代替
    • 我们使用了 .OrderByDescending(x => x.WhateverProperty).First()

    推荐答案

    基本上,这个列表很大......它是相对正在处理的一小部分事情.不幸的是,泄漏抽象法则开始生效,每个提供商都有不同的答案..

    Basically, that list is huge... it is everything outside of the relatively small set of things that are handled. Unfortunately, the Law Of Leaky Abstractions kicks in, and each provider has different answers...

    LINQ-to-Objects 可以做任何事情(几乎),因为它是委托;LINQ-to-SQL 和实体框架具有不同的支持集.

    LINQ-to-Objects will do anything (pretty much), since it is delegates; LINQ-to-SQL and Entity Framework have different sets of support.

    总的来说,我在使用 DateTime 属性等方面取得了相当大的成功 - 但实际上,您必须确保单元测试涵盖了您的查询表达式,这样,如果您更改了提供程序(或提供程序更新),您就会知道这一切仍然有效.

    In general, I've had a fair amount of success using the DateTime properties etc - but in reality, you're going to have to ensure that your query expressions are covered by unit tests, so that if you ever change providers (or the provider gets updated) you know it all still works.

    我想一种观点是从 TSQL 的角度思考;没有BOTTOM n,但有一个TOP 1(关于OrderByDescending);就 string.IsNullOrEmpty 而言,您可能非常直白: foo.Bar == null ||foo.Bar == "";使用 DateTime.Date 您可能可以使用 DATEPART/各种组件做很多事情.

    I guess one view is to think in terms of TSQL; there is no BOTTOM n, but there is a TOP 1 (re the OrderByDescending); In terms of string.IsNullOrEmpty, you could be quite literal: foo.Bar == null || foo.Bar == ""; and with DateTime.Date you can probably do quite a bit with DATEPART / the various components.

    LINQ-to-SQL 的另一个选择是将逻辑封装在 UDF 中 - 这样您就可以编写一个接受 datetime 并返回一个 datetime 的 UDF,并且通过 dbml 将其公开到数据上下文中.然后您可以在查询中使用它:

    Another option with LINQ-to-SQL is to encapsulate the logic in a UDF - so you could write a UDF that takes a datetime and returns a datetime, and expose that via the dbml onto the data-context. You can then use that in your queries:

    where ctx.Date(foo.SomeDate) == DateTime.Today
    

    然而,这种方法不一定能很好地利用索引.

    This approach, however, doesn't necessarily make good use of indexes.

    更新:

    • 支持的方法翻译等在这里.
    • 支持的查询操作等在这里.

    有关完整的详细信息,您可以查看反射器中的 System.Data.Linq.SqlClient.PostBindDotNetConverter+Visitor - 特别是 Translate... 方法;一些 string 函数是单独处理的.所以不是一个巨大选择 - 但这是一个实现细节.

    For the full gory details, you can look at System.Data.Linq.SqlClient.PostBindDotNetConverter+Visitor in reflector - in particular the Translate... methods; some string functions are handled separately. So not a huge selection - but this is an implementation detail.

    这篇关于“不能在 DateTime 上调用方法",以及其他限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆