Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common Table Expression (CTE) #30757

Closed
arturwv opened this issue Apr 25, 2023 · 5 comments
Closed

Common Table Expression (CTE) #30757

arturwv opened this issue Apr 25, 2023 · 5 comments

Comments

@arturwv
Copy link

arturwv commented Apr 25, 2023

There is need to make recursive query to db. by LINQ
Defining recursive CTE


	WITH CTELocationsTree
	AS
	(
	SELECT [Id],[Name],[Code],[ParentId],[Discriminator],[Address],[Capacity],[something], Convert(NVARCHAR(MAX),[Id]) as HierarchyPath
	FROM [dbo].[Locations] WHERE Id = 1 

	UNION ALL

	SELECT loc.[Id],loc.[Name],loc.[Code],loc.[ParentId],loc.[Discriminator],loc.[Address],loc.[Capacity],loc.[something] , Convert(NVARCHAR(MAX),HierarchyPath +'/'+ Convert(NVARCHAR(20),loc.Id))  as HierarchyPath
	FROM [dbo].[Locations] loc
	INNER JOIN CTELocationsTree cloc  ON loc.ParentId = cloc.Id
	)
	SELECT * FROM CTELocationsTree cte 

Something like that (LINQ to DB)

https://linq2db.github.io/articles/sql/CTE.html?q=Common%20Table%20Expression%20(CTE)

That is create query by LINQ which will produce recursive CTE SqlQuery and map data to class.

Class example

  public abstract  class Location 
    {

        public int Id { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }

        public int? ParentId
        {
            get; set;
        }

        [ForeignKey("ParentId")]
        public virtual Location? Parent { get; set; }

        public virtual IEnumerable<Location>? Children { get; set; }
        public string? HierarchyPath { get; set ; }
    }

    public class Warehouse : Location
    {
        public int Capacity { get; set; }
        public int Something{ get; set; }
    }
@roji
Copy link
Member

roji commented Apr 25, 2023

Duplicate of #26486

@roji roji marked this as a duplicate of #26486 Apr 25, 2023
@roji
Copy link
Member

roji commented Apr 25, 2023

(or possibly #29918)

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Apr 25, 2023
@arturwv
Copy link
Author

arturwv commented Apr 25, 2023

so you won't, nice :/

@roji
Copy link
Member

roji commented Apr 25, 2023

@arturwv you just opened a duplicate issue - me closing it doesn't mean it won't get implemented (it's just not helpful to have two issues tracking the same thing).

In fact, I do think this is important and hope to be able to do it for EF Core 9.0.

@arturwv
Copy link
Author

arturwv commented Apr 25, 2023

thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants