0

Can someone please help me to create a linq query.

Let's assume I have one Linq query like below and I have the database context variable is there where all Tables are exists.

var pos = _context.PurchaseOrders
    .Join(_context.PODetails, poOrder=>poOrder.PurchaseOrderId, details => details.PurchaseOrderId,(poOrder, details) => new { poOrder, details })
    .Where(x => x.poOrder.Status != "PO Fully Invoiced" &&` CompareDates(x.details.ExpectedInvoiceDate.Value.AddMonths(validationAfter)));

CompareDates Method code:

bool CompareDates(DateTime expected) {
  var now = DateTime.Now;
  return expected.Year == now.Year && expected.Month == now.Month;
}

Question: Now I want to join another table called Budget and both budget and PurchaseOrder table can join using BudgetId.BudgetId column is already present in both PurchaseOrder and Budget Table. How to join the table to get all the data and also if possible, please explain the above linq query code or give any reference to follow.

  • 1
    1. When joining multiple tables it is usually easier to read / write that code using Linq instead of Lamda expressions. Example: `from po in _context.PurchaseOrders join detail in _context.PODetail on po.PurchaseOrderId equals detail.PurchaseOrderId join _context.OtherThing on xxxx where xxxx select xxx`. 2. `CompareDates` will not be translatable to a Sql store expression. – Igor Aug 03 '22 at 17:40
  • Can you please explain (poOrder, details) => new { poOrder, details }) this piece of code.Or written the code in Lambda expression. – Sambit Sahoo Aug 03 '22 at 17:51
  • See the answer in the 2nd marked duplicate: https://stackoverflow.com/a/2767742/1260204 – Igor Aug 03 '22 at 17:57
  • Thanks a lot. Your reference article helps me to resolve the issue. Keep sharing your suggestions. – Sambit Sahoo Aug 03 '22 at 20:17

0 Answers0