开发者

have join multiple table and get the output in DataRow in LINQ

开发者 https://www.devze.com 2023-02-19 07:38 出处:网络
HI, I have a scenario where i have join multiple table and get the output in DataRow(All the Rows return by the query).

HI,

I have a scenario where i have join multiple table and get the output in DataRow(All the Rows return by the query).

SQL Query:

SELECT  Fr.InterCodeId   
        FROM    
        CodeShareInterline Fr,    
        Airline A,Zone Z   #
        WHERE    
      开发者_开发百科  A.AirlineId = Fr.AirlineId   
        And Fr.ContractId=Z.ContractId

I know how to perform join in LINQ but how can i select all the column(Rows) in select statement of LINQ.


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
               };

The anonymous type contains all data you want, you can easily visit one column by:

result.FirstOrDefault().Zone.SomeField


This is untested but something close to this should work. Assuming your data context is call Context. This is a translation of what you have above.

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;

If you want to select all of the data then you need to do something like this.

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
                       };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号