Home > Back-end >  How place where condition in LINQ
How place where condition in LINQ

Time:04-21

I am fetching the data based on ID from multiple tables. Here is my code.

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
                               };  
}

How can I place the where condition in the above code and fetch the data based on unidID? The unidID is the ID which I am passing from another ActionResult Page.

CodePudding user response:

You can add Where after table2.ToList(), but please note that parentheses are required to treat the content of the search as a complete object.

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
                               }; 

Or

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()...
  • Related