我正在将wcf rest服务用于angular js application.I我正在尝试将三个linq查询连接到单个查询中并将记录显示到角度js应用程序中。但问题是当我输入帐号并单击提交按钮时,它也会一次又一次地显示相同的记录。而不是显示相同的记录我想显示空行。
这是Linq查询:
public string TranscationDetails(string Account_Number)
{
var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var inOut = context.Current_Account_Withdraw.Select(w => new {
w.Account_Number
,
Deposit = (decimal?)null
,
Withdrawal = (decimal?)w.Amount
,
w.Date
}).Concat(context.Current_Account_Deposit.Select(d => new {
d.Account_Number
,
Deposit = (decimal?)d.Amount
,
Withdrawal = (decimal?)null
,
d.Date
}))
.Where(r => r.Account_Number == accountNumber)
.OrderBy(r => r.Date)
.ToList();
// var summary = inOut.GroupBy(i => i.Account_Number).Select(g => new
// {
// Account_Number = g.Key
//,
// Balance = g.Sum(x => x.Deposit ?? 0M - x.Withdrawal ?? 0M)
//,
// TotalDeposits = g.Sum(x => x.Deposit ?? 0M)
//,
// TotalWithdrawals = g.Sum(x => x.Withdrawal ?? 0M)
// });
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Serialize(inOut); // return JSON string
}
}
}
}
这是我点击带帐号的按钮时屏幕截图,Linq查询也显示预期的record。我希望格兰特在总额底部的Money Money In和底部的Money Out总计以及账户记录。
但是,当我结合两个查询。
public string TranscationDetails(string Account_Number)
{
var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var inOut = context.Current_Account_Withdraw.Select(w => new {
w.Account_Number
,
Deposit = (decimal?)null
,
Withdrawal = (decimal?)w.Amount
,
w.Date
}).Concat(context.Current_Account_Deposit.Select(d => new {
d.Account_Number
,
Deposit = (decimal?)d.Amount
,
Withdrawal = (decimal?)null
,
d.Date
}))
.Where(r => r.Account_Number == accountNumber)
.OrderBy(r => r.Date)
.ToList();
var summary = inOut.GroupBy(i => i.Account_Number).Select(g => new
{
Account_Number = g.Key
,
Balance = g.Sum(x => x.Deposit ?? 0M - x.Withdrawal ?? 0M)
,
TotalDeposits = g.Sum(x => x.Deposit ?? 0M)
,
TotalWithdrawals = g.Sum(x => x.Withdrawal ?? 0M)
});
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Serialize(summary); // return JSON string
}
}
发生这种情况是因为您不止一次地引入相同的表。假设同一个Account_Details
项目有多个存款和多个提款,您需要按帐号对结果进行分组。
这是一个更正版本:
var CombinedQuery = (
from a in context.Current_Account_Details
join w in context.Current_Account_Withdraw on w.Account_Number equals a.Account_Number
join d in context.Current_Account_Deposits on d.Account_Number equals a.Account_Number
where a.Account_Number == accountNumber
group new {a, w, d} by a.Account_Number into g
select new {
Account_Number = g.Key
, DepositAmount = g.Sum(i => i.d.Amount)
, WithdrawalAmount = g.Sum(i => i.w.Amount)
, Account_Balance = g.Sum(i => i.a.Account_Balance)
}
).ToList();
首先,我想显示银行账户每月Money In和Money out声明等记录
然后你可以在没有分组或加入的情况下做到这一点,如下所示:
var inOut = context.Current_Account_Withdraw.Select(w => new {
w.Account_Number
, Deposit = (decimal?)null
, Withdrawal = (decimal?)w.Amount
, w.Date
}).Concat(context.Current_Account_Deposit.Select(d => new {
d.Account_Number
, Deposit = (decimal?)d.Amount
, Withdrawal = (decimal?)null
, d.Date
}))
.Where(r => r.Account_Number == accountNumber)
.OrderBy(r => r.Date)
.ToList();
上述查询连接存款和取款,并按日期订购。
现在您可以计算内存中的总数:
var summary = inOut.GroupBy(i => i.Account_Number).Select(g => new {
Account_Number = g.Key
, Balance = g.Sum(x => x.Deposit ?? 0M - x.Withdrawal ?? 0M)
, TotalDeposits = g.Sum(x => x.Deposit ?? 0M)
, TotalWithdrawals = g.Sum(x => x.Withdrawal ?? 0M)
});