简体   繁体   中英

Linq sum in Controller and pass to View ASP NET CORE 2.1

I want to ask how to write linq to sum in controller and add to view at table child ""

My model 1

public class ChartAccount
{
    [Key]
    public int ChartId { get; set; }
    public int Kodeakun { get; set; }
    public string Namaakun { get; set; }
    public int Saldo { get; set; }
    public virtual ICollection<Transaction> Transaksilist { get; set; }
}

model 2

public class Transaction
{   [Key]
    public int Id { get; set; }
    public string NoBukti { get; set; }
    public DateTime Tanggal { get; set; }
    public string Deskripsi { get; set; }

    [DisplayFormat(DataFormatString = "{0:C}")]
    public long Debit { get; set; }
    [DisplayFormat(DataFormatString = "{0:C}")]
    public long Credit{ get; set; }

    public int? ChartId { get; set; }
    public ChartAccount Transactions { get; set; }
}

I also want to fill table saldo with value in debit and kredit

In chartaccount view (index) (i fill with initial value at migration)

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Kodeakun)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Namaakun)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Saldo)
        </td>

it generated like this

|Kodeakun|Namaakun  |Saldo|
|10000   |Asset     |0    |
|20000   |Utang     |0    |
|30000   |Modal     |0    |
|40000   |Pendapatan|0    |

in transaction view (index)

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Transactions.Kodeakun)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Deskripsi)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Debit)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Credit)
        </td>

it generated like this

|Kodeakun|Debit     |Credit   |Deskripsi|
|10000   |1.000.000 |0        |Kas      |
|20000   |0         |1.000.000|Utang    |

I want to write linq in controller. what method i suppose to write, in index or detail in chartaccount controller?

    // GET: ChartAccounts
    public async Task<IActionResult> Index()
    {
        return View(await _context.Accounts.ToListAsync());
    }

    // GET: ChartAccounts/Details/5
    public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var chartAccount = await _context.Accounts
            .FirstOrDefaultAsync(m => m.ChartId == id);
        if (chartAccount == null)
        {
            return NotFound();
        }

        return View(chartAccount);
    }

I tried but I little don't understand how to pass to view

Code I try in controller detail

        //public DbSet<Transaction> Transaksi { get; set; }
        //public DbSet<ChartAccount> Accounts { get; set; }
        var a = from s in _context.Transaksi
                group s by s.Transactions.Kodeakun;

So basically I want to if I insert data in transaction and save it. I also want it update data chartaccount in example

|Kodeakun|Debit     |Credit   |Deskripsi|
|10000   |1.000.000 |0        |Kas      |
|20000   |0         |1.000.000|Utang    |
|10000   |0         |1.000.000|Kas      |
|10000   |1.000.000 |0        |Kas      |

it has debit kredit. so my chart account should like this

|Kodeakun|Namaakun  |Saldo    |
|10000   |Asset     |1.000.000|
|20000   |Utang     |1.000.000|
|30000   |Modal     |0        |
|40000   |Pendapatan|0        |

Basically Debit is (+) and Kredit is (-), so i have insert 2 Kas, which in debit and kredit, and the result in saldo is 1.000.000 (2.000.000-1.000.000)

Sorry for my English

You have to use GroupBy with sum and constructing a new object:

data.GroupBy(el => el.Kodeakun).Select(group => new ChartAccount
        {
            Saldo = group.Sum(item => item.Debit) - group.Sum(item => item.Credit),
            Kodeakun = group.Key,
            Namaakun = // take it from appropriate field
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM