Answered How to use "Union" in LINQ

  • Wednesday, May 16, 2012 9:41 AM
     
     

    Hi Team,

    I am new to LINQ. Could you please help me to change the below sql query to LINQ query.

    SELECT distinct D.InstanceName as Component, A.Environment, A.InstanceName, A.MountPoint, A.UtilisedGB, A.Utilised_Perc, A.AvailableGB
      FROM [EI].[StorageDetails_EI] A
     join [EI].[Server_Master_EI] B
     ON B.ServerName=A.InstanceName
     jOIN [EI].[Server_Details_EI] C
     ON C.SM_ID=B.SM_ID
     JOIN [EI].[ServerInstance_Master_EI] D
     ON D.SIM_ID=C.SIM_ID
     where A.Environment not in ('SA4','SA5')
     union
     SELECT distinct
     'LDA\ODS\DW' as Component
     , A.Environment
     , A.InstanceName
     , A.MountPoint
     , A.UtilisedGB
     , A.Utilised_Perc
     , A.AvailableGB
      FROM [EI].[StorageDetails_EI] A
     join [EI].[Server_Master_EI] B
     ON B.ServerName=A.InstanceName
     jOIN [EI].[Server_Details_EI] C
     ON C.SM_ID=B.SM_ID
     JOIN [EI].[ServerInstance_Master_EI] D
     ON D.SIM_ID=C.SIM_ID
     where A.Environment  in ('SA4','SA5')


    • Edited by nmittapalli Thursday, May 17, 2012 6:30 AM
    •  

All Replies

  • Saturday, May 19, 2012 6:18 AM
    Moderator
     
     Answered

    Hi nmittapalli,

    Welcome to MSDN Forum.

    In Linq query, there's a corresponding method Union. More information about this method, please refer to the link. Based on this issue, if the T-SQL is too complex, I suggest you to execute it directly in Linq to SQL, or write it as a stored procedure and execute it in Linq to SQL. Because Linq query will not be translated as the most effective T-SQL, if the T-SQL is too complex, and then we write a corresponding Linq query, after translation, the generated T-SQL may become more complex, it will certainly affect the performance. More information about how to execute T-SQL and Stored Procedure in Linq to SQL, please refer to the links below.

    How to: Directly Execute SQL Queries (LINQ to SQL)

    Walkthrough: Using Only Stored Procedures (C#) (LINQ to SQL)

    If you want further help, please feel free to let me know, I will do my best to help you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us