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

时间:2013-12-23 12:58:56

标签: c#-4.0 linq-to-sql

我想在linq查询中创建一个临时表。我已经搜索了解决方案,但没有成功。 这里“Node”是临时表,“Organization.TblOrganizationUnits”是我数据库中的表。 在linq中,如何创建临时表以及如何执行上述查询的不同连接和联合操作。 我的sql查询是:

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

0 个答案:

没有答案