[C#]使用 LINQ 创建 JSON

发布时间: 2017/2/27 7:17:39
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这是我的表结构︰mytablesctructure

我必须出示以下 JSON:

{
    Name: "AccCat1",
    Credit: 123,
    Debit: 123,
    AccountGroups:[
        {
            Name: "AccGrp1",
            Credit: 123,
            Debit: 123,
            Accounts: [
                {
                    Name: "Acc1",
                    Credit: 123,
                    Debit: 123,
                    AccountParticulars:[
                        {
                            Name: "AccPar1",
                            Credit: 123,
                            Debit: 123
                        },
                        {
                            Name: "AccPar2",
                            Credit: 123,
                            Debit: 123
                        }
                    ]
                }
            ]
        }
    ]
}

又要从 JournalRecord 开始,当我走在链创建 JSON 输出。这是我能鼓起为止,但可以看出这不是因为我总结信用,借记卡价值所有的方式,所以它被同样并不因此︰

var records = db.JournalEntries.Include(je => je.JournalRecords.Select(jr => jr.Account).Select(j => j.AccountParticulars))
            .Where(je => je.Date >= from && je.Date <= to)
            .SelectMany(s => s.JournalRecords)
            .GroupBy(d => d.AccountParticular.Account.AccountGroup.AccountCategory)
            .Select(g => new
            {
                Name = g.Key.Name,
                Credit = g.Sum(c => c.Credit),
                Debit = g.Sum(d => d.Debit),
                AccountGroups = g.Key.AccountGroups.Select(ag => new
                {
                    Name = ag.Name,
                    Credit = g.Sum(c => c.Credit),
                    Debit = g.Sum(d => d.Debit),
                    Accounts = ag.Accounts.Select(ac => new
                    {
                        Name = ac.Name,
                        Credit = g.Sum(c => c.Credit),
                        Debit = g.Sum(d => d.Debit),
                        AccountParticulars = ac.AccountParticulars.Select(ap => new 
                        {
                            Name = ap.Name,
                            Credit = g.Sum(c => c.Credit),
                            Debit = g.Sum(d => d.Debit)
                        })
                    })
                })
            });

这是我的代码的产生︰

[
  {
    "Name": "Cat1",
    "Credit": 11000, <--Total (correct)
    "Debit": 11000, <--Total (correct)
    "AccountGroups": [
      {
        "Name": "Grp1",
        "Credit": 11000, <--Total (correct)
        "Debit": 11000, <--Total (correct)
        "Accounts": [
          {
            "Name": "Acc1",
            "Credit": 11000, <--Total (correct)
            "Debit": 11000, <--Total (correct)
            "AccountParticulars": [
              {
                "Name": "AccPar1",
                "Credit": 11000, <-- Should be 500. From JournalRecord
                "Debit": 11000 <-- Should be 500. From JournalRecord
              },
              {
                "Name": "AccPar2",
                "Credit": 11000, <-- Should be 500. From JournalRecord
                "Debit": 11000 <-- Should be 500. From JournalRecord
              },
              {
                "Name": "Accpar3",
                "Credit": 11000, <-- Should be 10000. From JournalRecord
                "Debit": 11000 <-- Should be 10000. From JournalRecord
              }
            ]
          }
        ]
      }
    ]
  }
]

这是时间敏感的我,所以会帮助任何帮助向正确的方向。

解决方法 1:

你看起来要被总结在每一级相同的值。

在帐户详情一级,它看起来像你应该总结涉及个人帐户特别,即的值︰

AccountParticulars = ac.AccountParticulars.Select(ap => new 
{
    Name = ap.Name,
    Credit = g.Where(entry => ap.Id == entry.AccountParticularId).Sum(entry => entry.Credit),
    Debit = g.Where(entry => ap.Id == entry.AccountParticularId).Sum(entry => entry.Debit),
})
赞助商