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

查看:71
本文介绍了为什么LINQ查询和扩展不支持转换或舍入数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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无法识别''方法,并且该方法无法转换为商店表达式.

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值必须为1,001 C-1000.2,值必须为1,000 D-1080.588值必须为1,081 E-1010.45,该值必须为1,000

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

这是我的代码:

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

这是ViewModel:

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

这是ViewModel的类

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.

在Microsoft的 Math.Round参考页上已确认.

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

归结为,一半的值将四舍五入为偶数.

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

这篇关于为什么LINQ查询和扩展不支持转换或舍入数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆