Ali Eshghi
Ali Eshghi

Reputation: 1243

How write join with Entity Framework

Here is my select in SQL:

select * 
from FactorItems fi 
inner join tblparts p on p.PartsID = fi.PartRef

So as I use * to get all columns from my join.

But with this code:

var FactorItem = (from FI in context.FactorItems
                  join P in context.tblparts on FI.PartRef equals P.PartsID
                  where (FI.FactorRef == FactorID) 
                  select FI);

I just get data which is in FI. I want to get all data - so what code do I need for that?

Upvotes: 0

Views: 57

Answers (2)

Irf
Irf

Reputation: 4627

you are returning only one entity, FI. You need to return both the entities .. FI and P , like :

// your rest of query
select new { FI, P}

This way it should get you all the concerned data

Upvotes: 0

Rui Estreito
Rui Estreito

Reputation: 270

With that Linq to Sql code, you can return all data creating a new object:

var FactorItem = (from FI in context.FactorItems
                                  join P in context.tblparts on FI.PartRef equals P.PartsID
                                  where (FI.FactorRef == FactorID) 
                                  select new {FI,P};

Upvotes: 2

Related Questions