为什么Linq查询返回重复的列记录

时间:2017-12-20 02:54:39

标签: angularjs linq wcf

我在wsf rest服务中使用了角度js应用程序。我正在尝试将三个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查询也显示预期的enter image description here记录。 我希望在行底部的格兰特总金额和底部的资金总额以及帐户记录。

但是当我结合两个查询时。

   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




            }
        }

我得到了这个结果。enter image description here

1 个答案:

答案 0 :(得分:1)

这是因为您不止一次地引入相同的表。假设同一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)
});