【问题标题】:have join multiple table and get the output in DataRow in LINQ连接多个表并在 LINQ 的 DataRow 中获取输出
【发布时间】:2011-03-23 09:22:43
【问题描述】:

嗨,

我有一个场景,我连接多个表并在 DataRow 中获取输出(查询返回的所有行)。

SQL 查询:

SELECT  Fr.InterCodeId   
        FROM    
        CodeShareInterline Fr,    
        Airline A,Zone Z   #
        WHERE    
        A.AirlineId = Fr.AirlineId   
        And Fr.ContractId=Z.ContractId

我知道如何在 LINQ 中执行连接,但是如何在 LINQ 的 select 语句中选择所有列(行)。

【问题讨论】:

    标签: c# linq linq-to-dataset


    【解决方案1】:
    var result = from fr in dataContext.CodeShareInterline
                from a in dataContext.AirLine
                from z in dataContext.Zone
                where a.AirlineId == fr.AirlineId && fr.ContractId == z.ContractId
                select new 
                   {
                       Interline = fr,
                       AirLine = a,
                       Zone = z
                   };
    

    匿名类型包含您想要的所有数据,您可以通过以下方式轻松访问一列:

    result.FirstOrDefault().Zone.SomeField
    

    【讨论】:

    • 我认为在您的两个解决方案中,您都缺少他的要求,即投影到 DataRow 中。
    【解决方案2】:

    这是未经测试的,但接近这个的东西应该可以工作。假设您的数据上下文是调用上下文。这是你上面的翻译。

    var o = from fr in Context.CodeShareInterline
                join a from Context.Airline on a.AirlineId == fr.AirlineId 
                join z from Context.Zone on fr.ContactId == z.ContactId
                select fr.InterCodeId;
    

    如果你想选择所有数据,那么你需要做这样的事情。

    var o = from fr in Context.CodeShareInterline
                join a from Context.Airline on a.AirlineId == fr.AirlineId 
                join z from Context.Zone on fr.ContactId == z.ContactId
                select new { 
                            Interline = fr,
                            AirLine = a,
                            Zone = z
                           };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2010-10-24
      相关资源
      最近更新 更多