为什么 LINQ 查询和扩展不支持转换或舍入数字?



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

LINQ to 实体无法识别方法 '' 方法,并且此方法无法转换为存储表达式。

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

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();

这是视图模型的类:

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(如果必须为空,则decimal?(数据类型,而不是long(您也不应该使用floatdouble因为听起来您正在处理需要更高精度的数字(。 您的 B/C/D/E 值应为 decimal? S。

这可能是由于十进制的默认舍入模式,该模式遵循 IEEE 标准 754 第 4.1 节,其中(部分(:

本标准的实现应提供四舍五入到最接近作为 默认舍入模式。在此模式下,最接近 应提供无限精确的结果;如果两个最接近的可表示 值相等接近,其最低有效位为零的值应为 交付。

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

归根结底,半值将四舍五入到偶数。

最新更新