如何用c#linq中的另一个查询结果减去查询结果?

时间:2017-01-19 22:36:54

标签: c# linq

我想从investment获取记录,并计算Amount investment_Line的总和investment_Line.ParentID==investment.Investment

减去金额Calculte==d.Amount-Answer

var a = (from investment in _entities.Investments
        where investment.StatusID == 4
        select new
        {
            RefNo = investment.RefNo,
            InvestedAmount = investment.InvestedAmount,
            Amount = investment.InvestedAmount + investment.ProfitAmount,
            InvestmentID = investment.ID,
            UserID = investment.UserID,
            StatusID = investment.StatusID,

        }).ToList();

if (a.Count() != 0)
{
    foreach (var row in a)
    {
        var count = (_entities.Investment_Line.Where(x => x.ParentID == row.InvestmentID)).Count();
        if(count != 0)
        {
            var _group = _entities.Investment_Line.Where(x => x.ParentID == row.InvestmentID).GroupBy(g => g.ParentID).Select(s => new { SumAmount = s.Sum(sum => sum.Amount) });
            double answer = double.ConvertToDouble(_group.Select(x => x.SumAmount));
            var data = (from d in a
                        select new Search.ReadyInvestmentList
                        {
                            RefNo = d.RefNo,
                            InvestedAmount = d.InvestedAmount,
                            TotalAmount = d.Amount,
                            InvestmentID = d.InvestmentID,
                            UserID = d.UserID,
                            StatusID = d.StatusID,
                            Calculate = d.Amount-answer
                        }).ToList();
        }
    }
}

投资表

+---+--------------------------+--------+-------------+----------------+--------+-------------+----------+
|ID | CreateDate               | UserID |  RefNo      | InvestedAmount | Growth%| ProfitAmount|StatusID  |
+---+--------------------------+--------+-------------+----------------+--------+-------------+----------+
|1  | 2017-01-11 16:39:06.483  | 1      |  BPWM57G2Q2 | 20000          | 30     | 6000        | 4        |
|2  | 2017-01-11 16:49:18.850  | 2      |  BPWM56H2T0 | 10000          | 30     | 3000        | 4        |
|3  | 2017-01-11 17:15:02.667  | 3      |  BPWM56G2L0 | 500            | 30     | 1500        | 1        |
|4  | 2017-01-12 20:22:02.160  | 5      |  BPWM68L2I0 | 500            | 30     | 150         | 2        |
|5  | 2017-01-12 20:25:03.160  | 5      |  BPWM63F2I5 | 2000           | 30     | 600         | 4        |
+---+--------------------------+--------+-------------+----------------+--------+-------------+----------+

Investment_Line表

+----+-------------+-----------+-----------+-------+------------+-----------+
| ID | InvestmentID| InvestorID| AssistorID| Amount| RefNo      |  ParentID |
+----+-------------+-----------+-----------+-------+------------+-----------+
| 1  | 2           | 2         | 1         | 10000 | BPWM56H2T0 |  1        |
| 2  | 3           | 3         | 1         | 5000  | BPWM56G2L0 |  1        |
| 3  | 4           | 5         | 3         | 500   | BPWM68L2I0 |  3        |
+----+-------------+-----------+-----------+-------+------------+-----------+

所需输出

+----+---------+-----------+-------+---------------+------------+-----------+
| InvestmentID | RefNo     | UserID| InvestedAmount| TotalAmount| Calculate |
+--------------+--------------------+--------------+------------+-----------+
| 1            | BPWM57G2Q2| 1     | 20000         | 26000      | 15000     |
| 2            | BPWM56H2T0| 2     | 10000         | 13000      | 0         |
| 5            | BPWM63F2I5| 5     | 2000          | 2600       | 0         |
+----+---------+-----------+-------+-------------- +------------+-----------+

最好的方法是什么? 请帮忙。

1 个答案:

答案 0 :(得分:0)

如果主详细信息表中有许多记录,您的代码将会有糟糕的性能。因为你将有masterRecordCount * DetailRecordCount嵌套循环。因此,如果您将一个查询分组并加入

会更好
var calculatedlist = from I in _entities.Investments
            join IL in _entities.Investment_Line on I.ID equals IL.ParentID
            group IL by new { I.ID } into g
            select new { 
                InvestmentID  = g.Key,
                RefNo      = g.Max(m => m.I.RefNo), 
                UserID = g.Max(m => m.I.UserID) ,
                InvestedAmount = g.Max(m => m.I.InvestedAmount) ,
                TotalAmount = g.Max(m => m.I.InvestedAmount) + g.Max(m => m.I.ProfitAmount),
                Calculate  = g.Max(m => m.I.InvestedAmount) + g.Max(m => m.I.ProfitAmount) - g.Sum(m => m.IL.Amount)
            };