LINQ 查询返回多个表
我有以下代码:
MyDataContext dc = new MyDataContext();
IQueryable<Table1> q1 =
from n in dc.Table1
select n
我想要做的是加入第二个表,因此:
var qry =
from n in dc.Table1
join r in dc.Table2 on n.Key equals r.Key
select new { n, r };
这将返回一种 IQueryable
类型。我现在想做的是提取Table1和Table2。例如(这显然行不通):
IQueryable<Table1> q1 = qry.Table1
IQueryable<Table2> q2 = qry.Table2
有办法做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想做这样的事情吗?
You want to do something like this?
当然有:
Sure there is:
您可以为表定义外键(在相应的
key
字段上),并将它们的关联添加到模型类中(最好通过 LINQ-to-SQL 设计器)。然后你可以这样做:
这样做的缺点是,即使使用 DataLoadOption,L2S 也会使成本变得非常昂贵。查询 Table2 以获取 Table1 结果集中的每一行。
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:
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.