问题
i have 3 tables
Table: A - aID - Text
Table: B - bID - Text
Table: A_B (reference table which holds both of the primary keys as foreign keys) - aID - bID
The Entity Framework knows that Table A_B is just a reference table and does not create it in the DBModel but keeps the references in Table A and B.
My question is how do i get the data which is in the reference table without actually having this table? When i try to access the values of Table B using the reference which is in Table A i cant get the values of the Table B Colums.
ObjectQuery<A> aTable = dbConnection.A;
var result = from data in aTable
where data.aID = '12'
select data.B; //B is the reference to table B out of table A
What i need is something like:
ObjectQuery<A> aTable = dbConnection.A;
var result = from data in aTable
where data.aID = '12'
select data.B.bID;
How do i get data out of Table B using the reference between Table A and Table B?
Using: VS 2010 Prof, .Net 4, Linq2Entity
回答1:
As Gary Said, you either use First (if you want one B for each A), FirstOrDefault (if you want nulls where there is no B for A) or SelectMany (if you want all Bs for each A) The latter can be written like this:
var result = from data in dbConnection.A
from b in data.B
select b.Id;
来源:https://stackoverflow.com/questions/9162844/linq2entity-getting-values-from-reference-table