我有一个有计算字段的对象;计算公式过于复杂,必须检查某些条件,并从其他对象加载一些数据(必须与其他表连接)。 这是我的对象:
public class LoadAndExitPermit : Master, IColorSource
{
private int? _ladingBillId;
[DataMember]
public virtual int? LadingBillID
{
get { return _ladingBillId; }
set { SetPropertyValue(ref _ladingBillId, value, "LadingBillID"); }
}
.
. {lots of Properties)
.
[DataMember]
public virtual ICollection<LoadAndExitPermitDocumentDetail> LoadAndExitPermitDocumentDetails { get; set; }
我的对象的重要部分是SumGoodsUnitPrice,它是:
[NotMapped, DataMember]
Public decimal? SumGoodsUnitPrice
{
get
{
if (LoadAndExitPermitDetails == null || !LoadAndExitPermitDetails.Any())
{
return -1;
}
if (LoadAndExitPermitDetails.First().RequestDetail.OrderDetail == null)
{
decimal? sum = 0m;
foreach (var item in LoadAndExitPermitDetails)
{
decimal? tarrif = item.Good.GoodsTariffDetails.Where(g =>
g.Price > 0 && g.IsActive == true && g.GoodsTariff.GoodsTariffType.IsPublic == true && g.GoodsTariff.FromDate < Date)
.OrderByDescending(w => w.GoodsTariff.FromDate).Select(c => c.Price).FirstOrDefault();
if (tarrif != null)
{
sum += ((item.Quantity ?? 0) * (1.09m * (tarrif))) ?? 0m;
}
else
{
decimal? lastTariff = item.Good.GoodsTariffDetails.Where(x => x.Price > 0 && x.IsActive == true
&& x.GoodsTariff.FromDate < Date).OrderByDescending(w => w.GoodsTariff.FromDate).Select(c => c.Price).FirstOrDefault() ?? 0m;
sum += ((item.Quantity ?? 0) * (1.09m * (lastTariff))) ?? 0;
}
}
return sum;
}
var z = LoadAndExitPermitDetails.Sum(l => (l.Quantity ?? 0) * (1.09m * (l.RequestDetail.OrderDetail.Quantity == 0 ? 0 : (l.RequestDetail.OrderDetail.Price / l.RequestDetail.OrderDetail.Quantity) ?? 0)));
return z;
}
private set
{
}
}
我有一些问题:
如果有人可以帮助我,我会这样做。
答案 0 :(得分:0)
如果我在哪里,我会添加一个在服务器上计算此字段的视图,并用于查询此视图。这与CQRS架构模式兼容。