简体   繁体   English

使用 LINQ 语句对 DateTime 字段使用 Where 子句

[英]Using Where clause for DateTime field using a LINQ statement

I am trying to return records for users based on their telephone numbers as well as a restriction to the PolicyEnd Field (DateTime Format) to return only those that are greater than or equal to 2022. However, I keep on running into several errors:我正在尝试根据用户的电话号码以及对PolicyEnd字段(日期时间格式)的限制来返回记录,以仅返回大于或等于 2022 的记录。但是,我不断遇到几个错误:

  1. && DateTime.ParseExact(s: ti0.Outer.Inner.PolicyEnd,format: "yyy-MM-dd",provider: __InvariantCulture_0) > DateTime.Now)' could not be translated. && DateTime.ParseExact(s: ti0.Outer.Inner.PolicyEnd,format: "yyy-MM-dd",provider: __InvariantCulture_0) > DateTime.Now)' 无法翻译。

     var QUERY = from client in _ipacontext.Inclients join policy in _ipacontext.Inpolicies on client.ClientId equals policy.AccountNo join types in _ipacontext.InpolicyTypes on policy.PolicyType equals types.TypeId where client.Telephone2 == "0000000" && DateTime.ParseExact(policy.PolicyEnd, "yyy-MM-dd", CultureInfo.InvariantCulture) > 2022

I have also tried this below but in vain:我也在下面尝试过,但徒劳无功:

where client.Telephone2 == "000000"  &&  Convert.ToDateTime(policy.PolicyEnd).Year >=2022

An example of the Date Format is as below:日期格式示例如下:

2022-08-31 00:00:00.000

Any help on other workarounds?对其他解决方法有任何帮助吗?

Dates have no format, they're binary types in all databases (except SQLite).日期没有格式,它们在所有数据库中都是二进制类型(SQLite 除外)。 SQL Server has date , datetime2 , datetimeoffset , time and the legacy datetime for storing dates and time-of-day. SQL 服务器具有datedatetime2datetimeoffsettime和用于存储日期和时间的旧datetime Storing dates as strings in a string field is a critical bug that must be fixed.将日期作为字符串存储在字符串字段中是一个必须修复的严重错误。 There's no way to control what goes into a string field, which means it's quite easy for garbage or strings with the wrong format to end up in the database.没有办法控制进入字符串字段的内容,这意味着垃圾或格式错误的字符串很容易进入数据库。

Trying to parse such strings will result in bad performance and increased blocking even if indexes are used.即使使用索引,尝试解析此类字符串也会导致性能下降并增加阻塞。 Indexes are built using the stored values, not function results.索引是使用存储的值构建的,而不是 function 结果。 Trying to parse PolicyEnd and filter by a specific date would have to scan the entire table, parse the values and only then decide which values to include.尝试解析PolicyEnd并按特定日期过滤将不得不扫描整个表,解析值,然后才决定要包含哪些值。 It will take Shared locks on the entire table while doing so, which would block any UPDATE or DELETE calls that tried to run at the same time, even if they were outside the date range.这样做时,它将在整个表上使用共享锁,这将阻止任何尝试同时运行的 UPDATE 或 DELETE 调用,即使它们超出了日期范围。

If the field uses a date type, the PolicyEnd property should be a DateTime .如果该字段使用日期类型,则PolicyEnd属性应该是DateTime In that casefiltering to find all dates after 2022 would be just:在那种情况下,过滤以查找 2022 年之后的所有日期将只是:

var fromDate=new DateTime(2023,1,1);

var query = ....
            where client.Telephone2 == "000000"  
                  && policy.PolicyEnd >=fromDate

This will result in a parameterized query that can use any indexes covering PolicyEnd to only touch policy rows whose PolicyEnd value matches the criteria.这将产生一个参数化查询,该查询可以使用任何涵盖PolicyEnd的索引来仅触及PolicyEnd值与条件匹配的策略行。

The JOINs aren't necessary either. JOIN 也不是必需的。 It's EF's job to generate the JOINs from the relations between entities.从实体之间的关系生成 JOIN 是 EF 的工作。 A Client should have a Policies collection. Client应该有一个Policies集合。 A Policy should have a PolicyType .一个Policy应该有一个PolicyType A LINQ query that returns clients without a second phone whose policies end in the future should be:一个 LINQ 查询返回没有第二部电话的客户,其政策在未来结束应该是:

var clients=from client in _context.Clients
            from policy in client.Policies
            where client.Telephone2 == "000000"  
                  && policy.PolicyEnd >=fromDate
            select ...;

Since your db table column format datetime, just try to use function由于您的数据库表列格式日期时间,只需尝试使用 function

var dt = new DateTime(2022,01,01);
....

 &&    EF.Functions.DateDiffYear(policy.PolicyEnd, dt) >= 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM