【问题标题】:Linq include (select * from a table)Linq 包含(从表中选择 *)
【发布时间】:2019-01-27 18:22:30
【问题描述】:

我想知道 linq 中的一个查询,它相当于以下内容:

select t1.*, (select columname from table2 t2 where id = 2)
from table1 t1

【问题讨论】:

标签: linq


【解决方案1】:

按照我的SQL to LINQ Recipe,但选择中的子查询略有变化:

var ans = from t1 in table
          select new {
              t1,
              columnname = (from t2 in table2
                            where id == 2
                            select columnname).FirstOrDefault()
          };

请注意,这将作为一个查询发送到 SQL。在这种情况下,将子查询放在另一个变量中会导致 LINQ 改为发送两个 SQL 查询。 SQL 要求在子查询中使用(隐含的)First,但如果您不介意 columnname 是可能具有多个值的 IEnumerable,则可以不使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    相关资源
    最近更新 更多