Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hello friends,
can you help me to create a temporary table in linq query.i have referred lots of sites but not getting the appropriate solution,finally i post my problem here

my sql query is:
C#
string query=string.Format(@"WITH Node (OrganizationUnitId, UnitName,ParentUnitId)
                                AS  (
                                    SELECT    Organization.TblOrganizationUnits.OrganizationUnitId, Organization.TblOrganizationUnits.UnitName , Organization.TblOrganizationUnits.ParentUnitId
                                    FROM       Organization.TblOrganizationUnits
                                    WHERE      OrganizationUnitId ={0}
                                    UNION ALL
                                    SELECT      Organization.TblOrganizationUnits.OrganizationUnitId,  Organization.TblOrganizationUnits.UnitName, Organization.TblOrganizationUnits.ParentUnitId
                                    FROM        Organization.TblOrganizationUnits
                                    INNER JOIN Node
                                    ON          Organization.TblOrganizationUnits.ParentUnitId = Node.OrganizationUnitId
                                    )
                                SELECT  OrganizationUnitId, UnitName,ParentUnitId FROM   Node
                                where  OrganizationUnitId not in (SELECT  ParentUnitId FROM   Node)
                               option (maxrecursion 0); ", OrganizationUnitId);


here "Node" is temporary table,and "Organization.TblOrganizationUnits" is table in my database.In sql it's easy to create a temporary table but in linq how can i create a temorary table and how i perform different joins and union operation of the above query.
kindly help me to fix it.
Thanks in Advance for helping me.
Posted
Comments
Hi Amit,

I guess you have rejected the answer by mistake.
If that is the case, please accept it again.

Thanks,
Tadit

Creating Temporary Table in LINQ is not possible. It is used to deal with existing Tables.
So, one way will be to deal LINQ with Stored Procedure and operate everything inside the Stored Procedure.

Refer- LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures)[^].
 
Share this answer
 
Amit, you should know that Node is not, in fact, a temporary table (although, I agree, in effect is works like it might as well be one). Node is a Common Table Expression. No storage is reserved for it, and it is only accessible to the statement in which it is declared.

Now, that doesn't begin to address your question - but (especially in our profession) it never hurts to be accurate in the names we use for things. And - it helps when you're searching for answers to questions: A quick Google for use CTE in linq to sql yielded (amongst others) the following: http://stackoverflow.com/questions/584841/common-table-expression-cte-in-linq-to-sql[^] which, in turn references How To: Directly Execute SQL Queries [in Linq-to-Sql][^].
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900