I have the following code:
MyDataContext dc = new MyDataContext();
IQueryable<Table1> q1 =
from n in dc.Table1
select n
What I want to be able to do is to join a second table, so:
var qry =
from n in dc.Table1
join r in dc.Table2 on n.Key equals r.Key
select new { n, r };
This returns me a type of IQueryable<anonymous>
. What I now want to do is extract Table1 and Table2. For example (this obviously doesn't work):
IQueryable<Table1> q1 = qry.Table1
IQueryable<Table2> q2 = qry.Table2
Is there a way to do this?
You want to do something like this?
IQueryable<Table1> q1 = qry.Select(x => x.n);
IQueryable<Table2> q2 = qry.Select(x => x.r);
Sure there is:
IQueryable<Table1> q1 = qry.Select(a => a.n);
IQueryable<Table2> q2 = qry.Select(a => a.r);
You can define foreign keys to your tables (on the respective key
fields) and add associations for them to your model clases (ideally via the LINQ-to-SQL designer).
Then you can do:
IQueryable<Table1> items1 = dc.Table1;
foreach (var item1 in items1)
{
var items2 = item1.Table2s; // naming might differ
}
The drawback with doing it that way, is that, even with DataLoadOption
s, L2S will make this very expensive. Querying Table2 for every row in the Table1 result set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.