我有一个
IQueryable<SomeObject> query
,其中包含一个属性 Value,它是 double
。
我想通过小数点前有千位分隔符逗号(欧洲)的字符串来过滤它。
我尝试过以下方法:
query.Where(q => q.Value.ToString("#,##0.00").StartsWith(someValue)
问题是无法翻译:
Translation of method 'double.ToString' failed. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
对于我的情况,不可能转换为客户评估,因为仍然需要将几个其他过滤器应用于我的查询。有什么办法可以解决这个问题吗
我认为
ToString()
不接受您的字符串格式。
将 double 字符串化并不是最好的主意,尤其是当它是一个适合使用逻辑的数字类型时。
更改您的:
query.Where(q => q.Value.ToString("#,##0.00").StartsWith(someValue);
到
query.Where(q => q.Value > someValue && q.Value < someMinimalValue);
然后,您想要采用“开头为”的值,因此值(对于
2.11
)可以是:
query.Where(q => q.Value >= 2.11 && q.Value < 2.12);
看到您只是测试值的开头,您可以尝试看看除法的结果是否是 10 的倍数:
query.Where(q => (q.Value / someValue) % 10 == 0 )