如何在 LINQ 中选择多个表?

Posted

技术标签:

【中文标题】如何在 LINQ 中选择多个表?【英文标题】:How do I select multiple tables in LINQ? 【发布时间】:2012-03-05 14:43:10 【问题描述】:

在 SQL 中我有:

 SELECT gu.*, cu.*
 FROM [gene_genunit] as gu, [cont_unitati] as cu
 WHERE gu.COD_UNIT = cu.COD_UNIT

我有一个 WPF 应用程序。

【问题讨论】:

【参考方案1】:

LINQ 等效项是

from gu in context.Gene
join cu in Context.Cont
on gu.Code_Unit equals cu.Code_Unit
select new 

   gu,
   cu

使用LinqPad 生成查询并学习 Linq

【讨论】:

“它没有提到'FROM 子句'”是什么意思? @Misi 忽略我对 From 子句的评论。看看代码就行了。【参考方案2】:
ARHIEntities ARHModel = new ARHIEntities(); // ARHIEntities is the model name
var qry = from gu in ARHModel.gene_genunit
          from cu in ARHModel.cont_unitati
          where gu.COD_UNIT == cu.COD_UNIT
          select new  cu, gu ;

编辑:添加了where 子句

【讨论】:

这是一个交叉连接,它与在sql中做左连接不同。

以上是关于如何在 LINQ 中选择多个表?的主要内容,如果未能解决你的问题,请参考以下文章