为什么LINQ查询和扩展不支持转换或四舍五入的数字?[英] Why LINQ Query and Extension don't support conversion or rounding numbers?

本文是小编为大家收集整理的关于为什么LINQ查询和扩展不支持转换或四舍五入的数字?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我尝试四舍五入,格式化数字.但它似乎在 LINQ 中不起作用.我需要格式化,并将数字四舍五入,如下例所示,作为您的参考.谢谢你的帮助.我得到了例外.

LINQ to Entities 无法识别方法 '' 方法,并且该方法无法转换为存储表达式.

匿名类型对于将非十进制数四舍五入很重要,格式为逗号分隔:

B - 1000.5 该值必须为 1,001C - 1000.2 该值必须为 1,000D - 1080.588 该值必须为 1,081E - 1010.45 该值必须为 1,000

这是我的代码:

 var result = _context.DwPropertyMasters.Where(x => x.ShowMapPoint == "Y")
                .Select(x => new
                {
                    x.LandId,
                    a = x.Development == null || x.Development == "" ? x.Location : x.Development,
                    x.MapPointX,
                    x.MapPointY,
                    AreaSize = x.AreaSize ?? 0,
                    Premium = x.Premium ?? 0,
                    b = (x.Premium == 0 ? null : x.Premium) * 100000000 / (x.AreaSize == 0 ? null : x.AreaSize) ?? 0,
                    c =
                    _context.DwPropertyDetails.Where(
                            z => (z.TransactionPrice > 0 || z.TransactionPrice != null) && z.LandId == x.LandId)
                        .GroupBy(z => z.LandId)
                        .Select(g =>
                            (g.Sum(p => p.TransactionPrice) == 0 ? null : g.Sum(p => p.TransactionPrice)) /
                            (g.Sum(p => p.ActualSize) == 0 ? null : g.Sum(p => p.ActualSize)) ?? 0)
                        .FirstOrDefault(),
                    d =
                    ((x.AreaSize2 == 0 ? null : x.AreaSize2) == 0
                        ? 0
                        : (x.Premium == 0 ? null : x.Premium) * 100000000 / (x.AreaSize2 == 0 ? null : x.AreaSize2)) ??
                    0,
                    x.LandType,
                    e =
                    _context.DwPropertyDetails.Where(
                            y => (y.TransactionPrice > 0 || y.TransactionPrice != null) && y.LandId == x.LandId)
                        .Select(y => new
                        {
                            a = 1
                        }).Count()
                });

这是视图模型:

 var output = result.Select(x => new SearchViewModels
            {
                LandId = x.LandId,
                A = x.a,
                MapPointX = x.MapPointX,
                MapPointY = x.MapPointY,
                AreaSize = x.AreaSize,
                Premium = x.Premium,
                B = x.b,
                C = x.c,
                D = x.d,
                LandType = x.LandType,
                E = x.e
            }).ToArray();

这是 ViewModel 的类:

public class SearchViewModels
    {
        public long LandId { get; set; }
        public string A { get; set; }
        public string MapPointX { get; set; }
        public string MapPointY { get; set; }
        public long? AreaSize { get; set; }
        public long? Premium { get; set; }
        public long? B { get; set; }
        public long? C { get; set; }
        public long? D { get; set; }
        public string LandType { get; set; }
        public long? E { get; set; }
    }

推荐答案

这可能是由于 Decimal 的默认舍入模式,遵循 IEEE 标准 754,第 4.1 节(部分)说明:

<块引用>

本标准的实施应提供四舍五入,因为默认舍入模式.在这种模式下,可表示的值最接近将提供无限精确的结果;如果两个最近的可表示值同样接近,其最低有效位为零的应为已送达.

这已在 Microsoft 的 Math.Round 参考页面上得到证实.

归结为一半的值将向偶数舍入.

本文地址:https://www.itbaoku.cn/post/1556938.html

问题描述

I try to round, format the numbers. But it doesn't seem to work in LINQ. I need to format, and round the numbers as the example below as your reference. Thank you for help. Exception I get.

LINQ to Entities does not recognize the method '' method, and this method cannot be translated into a store expression.

The anonymous type is important to round non decimal number and format to comma separated:

B - 1000.5 the value must 1,001 C - 1000.2 the value must 1,000 D - 1080.588 the value must 1,081 E - 1010.45 the value must 1,000

Here's my code:

 var result = _context.DwPropertyMasters.Where(x => x.ShowMapPoint == "Y")
                .Select(x => new
                {
                    x.LandId,
                    a = x.Development == null || x.Development == "" ? x.Location : x.Development,
                    x.MapPointX,
                    x.MapPointY,
                    AreaSize = x.AreaSize ?? 0,
                    Premium = x.Premium ?? 0,
                    b = (x.Premium == 0 ? null : x.Premium) * 100000000 / (x.AreaSize == 0 ? null : x.AreaSize) ?? 0,
                    c =
                    _context.DwPropertyDetails.Where(
                            z => (z.TransactionPrice > 0 || z.TransactionPrice != null) && z.LandId == x.LandId)
                        .GroupBy(z => z.LandId)
                        .Select(g =>
                            (g.Sum(p => p.TransactionPrice) == 0 ? null : g.Sum(p => p.TransactionPrice)) /
                            (g.Sum(p => p.ActualSize) == 0 ? null : g.Sum(p => p.ActualSize)) ?? 0)
                        .FirstOrDefault(),
                    d =
                    ((x.AreaSize2 == 0 ? null : x.AreaSize2) == 0
                        ? 0
                        : (x.Premium == 0 ? null : x.Premium) * 100000000 / (x.AreaSize2 == 0 ? null : x.AreaSize2)) ??
                    0,
                    x.LandType,
                    e =
                    _context.DwPropertyDetails.Where(
                            y => (y.TransactionPrice > 0 || y.TransactionPrice != null) && y.LandId == x.LandId)
                        .Select(y => new
                        {
                            a = 1
                        }).Count()
                });

This is the ViewModel:

 var output = result.Select(x => new SearchViewModels
            {
                LandId = x.LandId,
                A = x.a,
                MapPointX = x.MapPointX,
                MapPointY = x.MapPointY,
                AreaSize = x.AreaSize,
                Premium = x.Premium,
                B = x.b,
                C = x.c,
                D = x.d,
                LandType = x.LandType,
                E = x.e
            }).ToArray();

This is the Class of ViewModel:

public class SearchViewModels
    {
        public long LandId { get; set; }
        public string A { get; set; }
        public string MapPointX { get; set; }
        public string MapPointY { get; set; }
        public long? AreaSize { get; set; }
        public long? Premium { get; set; }
        public long? B { get; set; }
        public long? C { get; set; }
        public long? D { get; set; }
        public string LandType { get; set; }
        public long? E { get; set; }
    }

推荐答案

This is likely to be due to the default rounding mode for Decimal, which follows IEEE Standard 754, section 4.1 which states (in part):

An implementation of this standard shall provide round to nearest as the default rounding mode. In this mode the representable value nearest to the infinitely precise result shall be delivered; if the two nearest representable values are equally near, the one with its least significant bit zero shall be delivered.

This is confirmed at Microsoft's Math.Round reference page.

What it boils down to is that half values will round towards even numbers.

相关标签/搜索