如何在 LINQ 中放置条件

How place where condition in LINQ

我正在从多个表中获取基于 ID 的数据。这是我的代码。

public async Task<IActionResult> GeneratePDF(string uniqID)
{
//Other code
 List<MasterDetails> MasterData = await _dbInvoiceInfoContext.GetAllInvoiceAsync();
            CustData.CustInvoiceInfo = MasterData[0].CustInvoiceInfo;
            CustData.CustInvoiceTotalInfo = MasterData[0].CustInvoiceTotalInfo;

 List<MasterDetails> MasterData1 = await _dbClientInfoContext.GetAllClientAsync();
            CustData.CustClientInfo = MasterData1[0].CustClientInfo;
            CustData.CustClientAddress = MasterData1[0].CustClientAddress;

  IEnumerable<MasterDetails> clientRecord = from c in CustData.CustInvoiceInfo
                               join a in CustData.CustInvoiceTotalInfo on
                               c.InvoiceUniqueID equals a.InvoiceId into table1
                               from a in table1.ToList()                             
                               join b in CustData.CustClientInfo on
                               c.InvoiceClientId equals b.ClientId into table2
                               from b in table2.ToList()
                               select new MasterDetails
                               {
                                   finalInvoiceInfo = c,
                                   finalInvoiceTotalInfo = a,
                                   finalClientInfo = b
                               };  
}

如何在上述代码中放置where条件,根据unidID取数据? unidID 是我从另一个 ActionResult 页面传递的 ID。

您可以在table2.ToList()后加上Where,但请注意括号是必须的,将搜索的内容视为一个完整的对象。

IEnumerable<MasterDetails> clientRecord = (from c in CustData.CustInvoiceInfo
                               join a in CustData.CustInvoiceTotalInfo on
                               c.InvoiceUniqueID equals a.InvoiceId into table1
                               from a in table1.ToList()                             
                               join b in CustData.CustClientInfo on
                               c.InvoiceClientId equals b.ClientId into table2
                               from b in table2.ToList()).Where(a=>a.unidID=unidID)
                               select new MasterDetails
                               {
                                   finalInvoiceInfo = c,
                                   finalInvoiceTotalInfo = a,
                                   finalClientInfo = b
                               }; 

或者

IEnumerable<MasterDetails> clientRecord = from c in CustData.CustInvoiceInfo
                               join a in CustData.CustInvoiceTotalInfo on
                               c.InvoiceUniqueID equals a.InvoiceId into table1
                               from a in table1.ToList()                             
                               join b in CustData.CustClientInfo on
                               c.InvoiceClientId equals b.ClientId into table2
                               from b in table2.ToList()
                               select new MasterDetails
                               {
                                   finalInvoiceInfo = c,
                                   finalInvoiceTotalInfo = a,
                                   finalClientInfo = b
                               };
var data=clientRecord.Where()...