Linq recursive query get all siblings
up vote
0
down vote
favorite
The objective is to make a Linq query to the database that gets the rest of ids that link back the input id in the method called GetFolder
Sample Data
Id parentId
10 9
11 10
12 10
13 12
14 13
15 13
100 20
Expected Output
Id parentId
10 11
12 10
13 12
14 13
public class Folder
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id get; set;
[Column("Parent_Id")]
public int? ParentId get; set;
public virtual ICollection<Folder> Children get; set;
Have the ICollection allows me to go one view the siblings of that folder.
GetFolders methods return these:
Id parentId
10 11
11 10
12 10
private async Task<Folder> GetFolder(int folderId)
var entity = await Database.Folders
.Where(e => e.Id == folderId)
.Include(e => e.Children)
.SingleOrDefaultAsync();
return entity;
I tried to write a recursive function to get the rest of the Ids but it doesn't work correctly.
Does anyone have any recommends on how to write this recursive function to receive the rest of the Ids.
c# linq
|
show 6 more comments
up vote
0
down vote
favorite
The objective is to make a Linq query to the database that gets the rest of ids that link back the input id in the method called GetFolder
Sample Data
Id parentId
10 9
11 10
12 10
13 12
14 13
15 13
100 20
Expected Output
Id parentId
10 11
12 10
13 12
14 13
public class Folder
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id get; set;
[Column("Parent_Id")]
public int? ParentId get; set;
public virtual ICollection<Folder> Children get; set;
Have the ICollection allows me to go one view the siblings of that folder.
GetFolders methods return these:
Id parentId
10 11
11 10
12 10
private async Task<Folder> GetFolder(int folderId)
var entity = await Database.Folders
.Where(e => e.Id == folderId)
.Include(e => e.Children)
.SingleOrDefaultAsync();
return entity;
I tried to write a recursive function to get the rest of the Ids but it doesn't work correctly.
Does anyone have any recommends on how to write this recursive function to receive the rest of the Ids.
c# linq
In your example, 10 and 11 are eachother's parents - is that intentional? If so you'll have to ensure the recursion doesn't end up in a loop.
– C.Evenhuis
Nov 10 at 18:40
a recursive function is a function which calls itself. This function suppose to haveint folderId
as input parameter. With each call you need to do something with input parameter , for example do increasing ... but you can't increase value infinitely, you have to set a limit
– Z.R.T.
Nov 10 at 18:44
that was a mistake. what i tried to do was add all the values returned for the linq into a hash list and then do a foreach (var id inListId.tolist) GetFolder and it will keep all until all the values are obtained but it did not work and is very messy coding wise. i dont know the limit so thats the issue with this
– user10609979
Nov 10 at 18:51
do you know the max of parentId at the moment when you call GetFolder ?
– Z.R.T.
Nov 10 at 18:54
i dont know the max limit so thats the issue with this. i get the folderid from clicking on the folder button on the front end. this is basically the same of the folder structure on windows but i dont know how deep the folder structure goes.
– user10609979
Nov 10 at 18:57
|
show 6 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The objective is to make a Linq query to the database that gets the rest of ids that link back the input id in the method called GetFolder
Sample Data
Id parentId
10 9
11 10
12 10
13 12
14 13
15 13
100 20
Expected Output
Id parentId
10 11
12 10
13 12
14 13
public class Folder
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id get; set;
[Column("Parent_Id")]
public int? ParentId get; set;
public virtual ICollection<Folder> Children get; set;
Have the ICollection allows me to go one view the siblings of that folder.
GetFolders methods return these:
Id parentId
10 11
11 10
12 10
private async Task<Folder> GetFolder(int folderId)
var entity = await Database.Folders
.Where(e => e.Id == folderId)
.Include(e => e.Children)
.SingleOrDefaultAsync();
return entity;
I tried to write a recursive function to get the rest of the Ids but it doesn't work correctly.
Does anyone have any recommends on how to write this recursive function to receive the rest of the Ids.
c# linq
The objective is to make a Linq query to the database that gets the rest of ids that link back the input id in the method called GetFolder
Sample Data
Id parentId
10 9
11 10
12 10
13 12
14 13
15 13
100 20
Expected Output
Id parentId
10 11
12 10
13 12
14 13
public class Folder
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id get; set;
[Column("Parent_Id")]
public int? ParentId get; set;
public virtual ICollection<Folder> Children get; set;
Have the ICollection allows me to go one view the siblings of that folder.
GetFolders methods return these:
Id parentId
10 11
11 10
12 10
private async Task<Folder> GetFolder(int folderId)
var entity = await Database.Folders
.Where(e => e.Id == folderId)
.Include(e => e.Children)
.SingleOrDefaultAsync();
return entity;
I tried to write a recursive function to get the rest of the Ids but it doesn't work correctly.
Does anyone have any recommends on how to write this recursive function to receive the rest of the Ids.
c# linq
c# linq
edited Nov 10 at 18:48
asked Nov 10 at 17:36
user10609979
11
11
In your example, 10 and 11 are eachother's parents - is that intentional? If so you'll have to ensure the recursion doesn't end up in a loop.
– C.Evenhuis
Nov 10 at 18:40
a recursive function is a function which calls itself. This function suppose to haveint folderId
as input parameter. With each call you need to do something with input parameter , for example do increasing ... but you can't increase value infinitely, you have to set a limit
– Z.R.T.
Nov 10 at 18:44
that was a mistake. what i tried to do was add all the values returned for the linq into a hash list and then do a foreach (var id inListId.tolist) GetFolder and it will keep all until all the values are obtained but it did not work and is very messy coding wise. i dont know the limit so thats the issue with this
– user10609979
Nov 10 at 18:51
do you know the max of parentId at the moment when you call GetFolder ?
– Z.R.T.
Nov 10 at 18:54
i dont know the max limit so thats the issue with this. i get the folderid from clicking on the folder button on the front end. this is basically the same of the folder structure on windows but i dont know how deep the folder structure goes.
– user10609979
Nov 10 at 18:57
|
show 6 more comments
In your example, 10 and 11 are eachother's parents - is that intentional? If so you'll have to ensure the recursion doesn't end up in a loop.
– C.Evenhuis
Nov 10 at 18:40
a recursive function is a function which calls itself. This function suppose to haveint folderId
as input parameter. With each call you need to do something with input parameter , for example do increasing ... but you can't increase value infinitely, you have to set a limit
– Z.R.T.
Nov 10 at 18:44
that was a mistake. what i tried to do was add all the values returned for the linq into a hash list and then do a foreach (var id inListId.tolist) GetFolder and it will keep all until all the values are obtained but it did not work and is very messy coding wise. i dont know the limit so thats the issue with this
– user10609979
Nov 10 at 18:51
do you know the max of parentId at the moment when you call GetFolder ?
– Z.R.T.
Nov 10 at 18:54
i dont know the max limit so thats the issue with this. i get the folderid from clicking on the folder button on the front end. this is basically the same of the folder structure on windows but i dont know how deep the folder structure goes.
– user10609979
Nov 10 at 18:57
In your example, 10 and 11 are eachother's parents - is that intentional? If so you'll have to ensure the recursion doesn't end up in a loop.
– C.Evenhuis
Nov 10 at 18:40
In your example, 10 and 11 are eachother's parents - is that intentional? If so you'll have to ensure the recursion doesn't end up in a loop.
– C.Evenhuis
Nov 10 at 18:40
a recursive function is a function which calls itself. This function suppose to have
int folderId
as input parameter. With each call you need to do something with input parameter , for example do increasing ... but you can't increase value infinitely, you have to set a limit– Z.R.T.
Nov 10 at 18:44
a recursive function is a function which calls itself. This function suppose to have
int folderId
as input parameter. With each call you need to do something with input parameter , for example do increasing ... but you can't increase value infinitely, you have to set a limit– Z.R.T.
Nov 10 at 18:44
that was a mistake. what i tried to do was add all the values returned for the linq into a hash list and then do a foreach (var id inListId.tolist) GetFolder and it will keep all until all the values are obtained but it did not work and is very messy coding wise. i dont know the limit so thats the issue with this
– user10609979
Nov 10 at 18:51
that was a mistake. what i tried to do was add all the values returned for the linq into a hash list and then do a foreach (var id inListId.tolist) GetFolder and it will keep all until all the values are obtained but it did not work and is very messy coding wise. i dont know the limit so thats the issue with this
– user10609979
Nov 10 at 18:51
do you know the max of parentId at the moment when you call GetFolder ?
– Z.R.T.
Nov 10 at 18:54
do you know the max of parentId at the moment when you call GetFolder ?
– Z.R.T.
Nov 10 at 18:54
i dont know the max limit so thats the issue with this. i get the folderid from clicking on the folder button on the front end. this is basically the same of the folder structure on windows but i dont know how deep the folder structure goes.
– user10609979
Nov 10 at 18:57
i dont know the max limit so thats the issue with this. i get the folderid from clicking on the folder button on the front end. this is basically the same of the folder structure on windows but i dont know how deep the folder structure goes.
– user10609979
Nov 10 at 18:57
|
show 6 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
How about
IEnumerable<int> GetRelatedIds(Folder root)
var ids = root.Children.SelectMany(c => GetRelatedIds(c)).ToList();
ids.Add(root.Id);
return ids;
This will also include the Id of the root folder. If you don't need it you can either just remove it from the list (it will be the last element), or run this method for all children of the root and concatenate the results.
yes i was trying something very similar to that so i will try an improve it.
– user10609979
Nov 10 at 19:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
How about
IEnumerable<int> GetRelatedIds(Folder root)
var ids = root.Children.SelectMany(c => GetRelatedIds(c)).ToList();
ids.Add(root.Id);
return ids;
This will also include the Id of the root folder. If you don't need it you can either just remove it from the list (it will be the last element), or run this method for all children of the root and concatenate the results.
yes i was trying something very similar to that so i will try an improve it.
– user10609979
Nov 10 at 19:41
add a comment |
up vote
0
down vote
How about
IEnumerable<int> GetRelatedIds(Folder root)
var ids = root.Children.SelectMany(c => GetRelatedIds(c)).ToList();
ids.Add(root.Id);
return ids;
This will also include the Id of the root folder. If you don't need it you can either just remove it from the list (it will be the last element), or run this method for all children of the root and concatenate the results.
yes i was trying something very similar to that so i will try an improve it.
– user10609979
Nov 10 at 19:41
add a comment |
up vote
0
down vote
up vote
0
down vote
How about
IEnumerable<int> GetRelatedIds(Folder root)
var ids = root.Children.SelectMany(c => GetRelatedIds(c)).ToList();
ids.Add(root.Id);
return ids;
This will also include the Id of the root folder. If you don't need it you can either just remove it from the list (it will be the last element), or run this method for all children of the root and concatenate the results.
How about
IEnumerable<int> GetRelatedIds(Folder root)
var ids = root.Children.SelectMany(c => GetRelatedIds(c)).ToList();
ids.Add(root.Id);
return ids;
This will also include the Id of the root folder. If you don't need it you can either just remove it from the list (it will be the last element), or run this method for all children of the root and concatenate the results.
answered Nov 10 at 19:23
V0ldek
2,134624
2,134624
yes i was trying something very similar to that so i will try an improve it.
– user10609979
Nov 10 at 19:41
add a comment |
yes i was trying something very similar to that so i will try an improve it.
– user10609979
Nov 10 at 19:41
yes i was trying something very similar to that so i will try an improve it.
– user10609979
Nov 10 at 19:41
yes i was trying something very similar to that so i will try an improve it.
– user10609979
Nov 10 at 19:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241657%2flinq-recursive-query-get-all-siblings%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In your example, 10 and 11 are eachother's parents - is that intentional? If so you'll have to ensure the recursion doesn't end up in a loop.
– C.Evenhuis
Nov 10 at 18:40
a recursive function is a function which calls itself. This function suppose to have
int folderId
as input parameter. With each call you need to do something with input parameter , for example do increasing ... but you can't increase value infinitely, you have to set a limit– Z.R.T.
Nov 10 at 18:44
that was a mistake. what i tried to do was add all the values returned for the linq into a hash list and then do a foreach (var id inListId.tolist) GetFolder and it will keep all until all the values are obtained but it did not work and is very messy coding wise. i dont know the limit so thats the issue with this
– user10609979
Nov 10 at 18:51
do you know the max of parentId at the moment when you call GetFolder ?
– Z.R.T.
Nov 10 at 18:54
i dont know the max limit so thats the issue with this. i get the folderid from clicking on the folder button on the front end. this is basically the same of the folder structure on windows but i dont know how deep the folder structure goes.
– user10609979
Nov 10 at 18:57