且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sql中创建一个临时表来进行linq查询

更新时间:2023-11-23 08:46:40

在LINQ中创建临时表是不可能的。它用于处理现有的表。

因此,一种方法是使用存储过程处理LINQ并操作存储过程中的所有内容。



参考 - LINQ to SQL(第6部分) - 使用存储过程检索数据) [ ^ ]。


Amit,您应该知道Node实际上不是临时表(虽然,我同意,实际上它的作用可能就像一个)。节点是公用表表达式。没有为它保留存储空间,它只能在声明它的语句中访问。



现在,这并没有开始解决你的问题 - 但是(特别是在我们的职业中)我们用于事物的名称准确无误。并且 - 当你在寻找问题的答案时它会有所帮助:一个快速的谷歌在linq中使用CTE到sql 产生(以及其他)以下内容:http://***.com/questions/584841/common-table-expression-cte- in-linq-to-sql [ ^ ]依次引用如何:直接执行SQL查询[在Linq-to-Sql] [ ^ ]。

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:

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.

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)[^].


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://***.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][^].