在 Asp.net Mvc 中使用 LINQ 查询自连接以获取使用同一表中的两行的结果

问题描述 投票:0回答:0

我想从表中获取记录。

我在表格中的每张发票都有 2 条记录,我想加入它们以便显示 1 条记录。

情况是这样的:我在一个表中有2条invoice记录,它们有相同的

GatePassNo
,但是一个有
totalAmount
,另一个有
null
totalAmount
中。

现在我想做的是当我获取没有

totalAmount
发票的记录时,我想从具有该值的发票中选择
totalAmount
值。

他们都有相同的GatePassNo但是他们有不同的

invoiceType
来区分他们。

我已经实现了,但是我实现它的方式不是最优的,我想要一个最优的解决方案。

 var query = _invoiceService.Table.Include("Items").Include("Customer").Include("Suppliers").Include("CreatedBy")
                                .Where(i => i.CompanyId == companyId);

switch (invoiceType)
            {
                case InvoiceType.Sales:
                    if (customerId != null)
                        query = query.Where(p => p.CustomerId == customerId);
                    if (deliveryOrderNo != null && deliveryOrderNo > 0)
                    {
                        var deliveryChallan = query.FirstOrDefault(p => p.InvoiceType == InvoiceType.DeliveryChallan && p.GatePassNo.ToString().EndsWith(deliveryOrderNo.ToString()));
                        query = deliveryChallan != null ? query.Where(p => p.InvoiceType == InvoiceType.Sales && p.GatePassNo == deliveryChallan.GatePassNo) : query.Where(p => p.InvoiceType == InvoiceType.Sales);
                    }
                    else
                        query = query.Where(p => p.InvoiceType == InvoiceType.Sales);
                    break;
               
                case InvoiceType.DeliveryChallan:
                    query = query.Where(p => p.InvoiceStatus != InvoiceStatus.Void);
                    if (invoiceNo != null && invoiceNo > 0)
                    {
                        var invoice = query.FirstOrDefault(p => p.InvoiceType == InvoiceType.Sales && p.InvoiceNo.ToString().EndsWith(invoiceNo.ToString()));

                        query = invoice != null ? query.Where(p => p.InvoiceType == InvoiceType.DeliveryChallan && p.GatePassNo == invoice.GatePassNo) : query.Where(p => p.InvoiceType == InvoiceType.DeliveryChallan);


                    }
                    else
                    {
                        query = query.Where(p => p.InvoiceType == InvoiceType.DeliveryChallan);

                        //Here i want to fetch the totalAmount from the invoice with the type sales and map them to the invoices in the above query.

                    }


                    if (customerId != null)
                        query = query.Where(p => p.CustomerId == customerId);
                    if (_workContextService.IsMaster() && customerId.HasValue)
                        showAll = true;
                    break;
            }

我仍然需要对查询进行一些排序,所以我不能在这里将它转换成 toList。我需要在不将其转换为列表的情况下执行此操作。

我尝试的是获取所有发票,然后创建一个函数再次获取销售类型的发票,然后将

totalAmount
映射到发票列表。

public void RetrieveTotalInvoiceAmount(List<Invoice> invoices)
        {
            foreach (var item in invoices)
            {
                var result = _invoiceService.Table.Where(i => i.GatePassNo == item.GatePassNo && i.InvoiceType == InvoiceType.Sales).FirstOrDefault();
                if (result != null)
                {
                    item.TotalAmount = result.TotalAmount;
                    
                }
            }
        }
c# asp.net-mvc linq self-join
© www.soinside.com 2019 - 2024. All rights reserved.