简体   繁体   English

使用Entity Framework Core和SQLite进行十进制/日期排序

[英]Decimal/Date Sorting With Entity Framework Core and SQLite

Using Entity Framework Core (Code First) and SQLite, Guid is stored as binary but Decimal and Date fields are stored as text with Microsoft's provider. 使用Entity Framework Core(Code First)和SQLite,Guid存储为二进制,但Decimal和Date字段作为文本存储在Microsoft的提供者中。

I can understand they might not want the imprecision of DOUBLE for currency amounts and thus use text. 我可以理解他们可能不希望货币金额的DOUBLE不精确,因此使用文本。

What happens if I need to sort? 如果我需要排序会发生什么? Is Entity Framework Core smart enough to make sorting work as expected (but slower because it needs to parse everything!), or will it sort alphabetically instead of sorting by number? 实体框架核心是否足够智能,使排序按预期工作(但速度较慢,因为它需要解析所有内容!),还是按字母顺序排序而不是按数字排序? I don't want it to return 100 before 2. 我不希望它在2之前返回100。

I'll have to do things like "give me the latest order" so what's the best approach for that? 我必须做“给我最新订单”这样的事情,那么最好的方法是什么? I want to make sure it's going to work. 我想确保它能够正常工作。

Am I better to switch to System.Data.SQLite provider to store dates in UNIX format (this is not supported by the Microsoft's provider)? 我是否最好切换到System.Data.SQLite提供程序以UNIX格式存储日期(Microsoft的提供程序不支持此功能)? and then would I have to do the parsing back and forth myself or it could take care of it automatically? 然后我必须自己来回解析它还是可以自动处理它?

I am still learning system.data.sqlite myself, but I am aware that you can create and assign a custom collation to your column. 我自己仍在学习system.data.sqlite,但我知道您可以为列创建并分配自定义排序规则。 The collation can either be assigned to the table column, or only to a particular view or query using standard sqlite SQL syntax and the COLLATE keyword. 可以使用标准sqlite SQL语法和COLLATE关键字将排序规则分配给表列,或仅分配给特定视图或查询。

This is not a complete example/tutorial, but for starters visit the Microsoft.data.sqlite docs . 这不是一个完整的示例/教程,但对于初学者来说,请访问Microsoft.data.sqlite文档 Also see this stack overflow answer . 另见这个堆栈溢出答案 These are just hints, but provide a consistent method to do this. 这些只是提示,但提供了一致的方法来执行此操作。 Remember that sqlite is an in-process DB engine, so it should still be rather efficient and still allow working with the database in a normal fashion without having to constantly inject custom logic between queries. 请记住,sqlite是一个进程内数据库引擎,所以它应该仍然相当高效,并且仍然允许以正常方式使用数据库,而不必在查询之间不断地注入自定义逻辑。 Once you have the custom collation defined and properly registered, it should be rather seamless with perhaps the only extra requirement to append eg COLLATE customDecimal to the ORDER BY clauses. 一旦定义并正确注册了自定义排序规则,它应该是相当无缝的,可能是附加的唯一额外要求,例如将COLLATE customDecimal附加到ORDER BY子句。

The custom collation function would convert the string value to an appropriate numeric type and return the comparison. 自定义归类函数会将字符串值转换为适当的数字类型并返回比较。 It's very similar to the native .Net IComparer and IComparison interfaces/implementations. 它与原生的.Net IComparer和IComparison接口/实现非常相似。

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

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