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.










share|improve this question









New contributor




user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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.










share|improve this question









New contributor




user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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.










share|improve this question









New contributor




user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 10 at 18:48





















New contributor




user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 17:36









user10609979

11




11




New contributor




user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user10609979 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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


















  • 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
















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












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.






share|improve this answer





















  • yes i was trying something very similar to that so i will try an improve it.
    – user10609979
    Nov 10 at 19:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






user10609979 is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • yes i was trying something very similar to that so i will try an improve it.
    – user10609979
    Nov 10 at 19:41















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.






share|improve this answer





















  • yes i was trying something very similar to that so i will try an improve it.
    – user10609979
    Nov 10 at 19:41













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 19:23









V0ldek

2,112624




2,112624












  • 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




yes i was trying something very similar to that so i will try an improve it.
– user10609979
Nov 10 at 19:41










user10609979 is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















user10609979 is a new contributor. Be nice, and check out our Code of Conduct.













user10609979 is a new contributor. Be nice, and check out our Code of Conduct.












user10609979 is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python