我嘗試四捨五入格式化數字。但它似乎並不適用於LINQ。我需要格式化,並以下面的示例爲參考進行舍入。謝謝你的幫助。例外,我得到。爲什麼LINQ查詢和擴展不支持轉換或舍入數字?
LINQ to Entities不能識別方法的方法,並且此方法不能被轉換成存儲表達式。
匿名類型是重要的,以四捨五入非十進制數和格式逗號分隔:
乙 - 1000.5值必須1001 Ç - 1000.2值必須1000 d - 1080.588值必須1081 ë - 1010.45值必須1000
這裏是我的代碼:
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; }
}
@ T.S。那個怎麼樣?我沒有想法。你可以幫我嗎? –