使用LINQ

时间:2019-04-08 12:23:45

标签: c# entity-framework linq

如何使用LINQ计算应收帐款。

我已经尝试过了,但是卡在了这里。 我已经在SQL中完成此操作,但我希望在LINQ中完成此操作,因此可以在我的MVC项目中使用它。

var sale = saleslist.GroupBy(s => s.BuyerId).Select(s => s.Sum(u => u.Amount)).ToList();
var receipt = receiptslist.GroupBy(r => r.StakeHolderId).Select(t => t.Sum(u => u.Amount)).ToList();

List<AccountReceivablesVM> res = db.StakeHolders
    .Where(r=>r.StakeHolderTypeId == "0b85a69e-55f2-4142-a49d-98e22aa7ca10")
    .Select(rvm => new AccountReceivablesVM
    {
        CompanyName = rvm.CompanyName,
        Receivables = //don't know what to do here
    }).ToList();

型号:

public class StakeHolder
{
    public string StakeHolderId { get; set; }
    public string CompanyName { get; set; }
    public string Address { get; set; }
    public string Contact { get; set; }
    public string StakeHolderTypeId { get; set; }
}


public class Sale
{
    public string SaleId { get; set; }
    public string RefNo { get; set; }
    public DateTime Date { get; set; }
    public string BuyerId { get; set; }
    public string Description { get; set; }
    public Nullable<double> Amount { get; set; }
}


public class PaymentsAndReceipt
{
    public string PaymentAndReceiptId { get; set; }
    public Nullable<int> VoucherNo { get; set; }
    public DateTime Date { get; set; }
    public string StakeHolderId { get; set; }
    public string Description { get; set; }
    public Nullable<double> Amount { get; set; }
}

public class AccountReceivablesVM
{
    public string CompanyName { get; set; }
    public Nullable<double> Receivables { get; set; }
}

enter image description here

enter image description here

预期结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以先使用利益相关者ID加入,然后将金额加总,然后使用公司名称和利益相关者ID分组,但是我用Linq编写了代码。我之所以将利益相关者ID视为表的主键,只是因为您没有提及利益相关者的架构。

PYSPARK_PYTHON=/usr/local/bin/pyenv2.7/bin/python spark-submit --master yarn-client --driver-memory 4g  --py-files /XXX/XXXX/XXXXX/pyFiles.zip --archives /XXX/XXXX/XXXX/sqls.zip#sqls main.py --sqls-path sqls