Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return List<Object> from Linq SQL (Lambda) with join and where

In C#, I need to retrieve a List<Object> using linq with lamba expression. This is the case:

List<TAB1> itemList = 
  context.TAB1.Join(
  context.TAB2, itm => itm.ItemCode, bcd => bcd.ItemCode, (itm, bcd) => new { ITM = itm, BCD = bcd })
                     .Where(i => i.ITM.ItemCode == (itemCode ?? i.ITM.ItemCode))
                     .Where(i => i.BCD.BcdCode.Contains(codeBars ?? i.BCD.BcdCode)).ToList();

At the moment that retrieves me a list<AnonymousType>, but I need a List<TAB1> that only return the values of that table.

EDIT: The returned List<TAB1> needs to replace one property (TAB1.Barcode) with TAB2.BcdCode (it's the same type). How can I do it?

like image 680
oldkefka Avatar asked Jun 25 '26 12:06

oldkefka


1 Answers

Add Select statement after filters:

List<TAB1> itemList = 
  context.TAB1.Join(
  context.TAB2, itm => itm.ItemCode, bcd => bcd.ItemCode, (itm, bcd) => new { ITM = itm, BCD = bcd })
                     .Where(i => i.ITM.ItemCode == (itemCode ?? i.ITM.ItemCode))
                     .Where(i => i.BCD.BcdCode.Contains(codeBars ?? i.BCD.BcdCode))
.Select(i => i.ITM)
.ToList();
like image 54
Backs Avatar answered Jun 27 '26 01:06

Backs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!