c# - How to join 2 tables using linq but avoid anonymous object -


i want join 2 tables using linq , avoid anonymous object.

so far use tuple.

var products = tm in db.targetmarkets                 join tp in db.products on tm.product_id equals tp.id                 tm.country == 2                 select tuple.create<targetmarket, product>(tm, tp); 

but, when foreach

foreach (var p in products) {     var = p.item1.id; } 

it throws exception

linq entities not recognize method 'system.tuple`2

question:

  1. is there way keep code typed
  2. what's wrong tuple (optional)

is there way keep code strong type

you can define new type , make object of type instead of anonymous object.

class producttargetmarket {    //attributes }   var productstargetmarkets = tm in db.targetmarkets             join tp in db.products on tm.product_id equals tp.id             tm.country == 2             select new producttargetmarket{attribute1ofproducttargetmarket = tp.attribute1, attribute1ofproducttargetmarket = tm.attribute1 }; 

to create tuple may first convert anonymous type , convert tuple, see this , this post.


Comments