![]() |
|
[C#] [Entity Framework] Multi level parent relation search. - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] [Entity Framework] Multi level parent relation search. (/showthread.php?tid=5818290) Pages: Pages
1
2
|
[Entity Framework] Multi level parent relation search. - severos - Mar 30, 2018 I have this entity: PHP Code PHP Code A category can be considered as Master/Primary if its parent is null. Lets say that I want to search Products using category ID. It's a trivial matter if I'm looking for a lowest child category. All I have to do is use the DbContext with the DbSet<Product> with a simple LINQ query to find Products that meet the category Id. But how would I go around writing a query that will search in sub categories? like if I search using a Master category id. I know I can use loops for it, but it doesn't seem like a nice solution to be honest, specially if you consider having multiple nesting levels. Anyone have an elegant looking code for this? I won't consider performance much to be honest since this is a small university project, but getting info on good performance solution would be awesome for future reading. RE: [Entity Framework] Multi level parent relation search. - Ninja - Mar 30, 2018 So you want to search for the last descendant of a Category, or am I misunderstanding it here? RE: [Entity Framework] Multi level parent relation search. - severos - Mar 30, 2018 (Mar 30, 2018 12:29 PM)Ninja Wrote: So you want to search for the last descendant of a Category, or am I misunderstanding it here? Given a category Id, I need to get all products under that category, or its sub categories. RE: [Entity Framework] Multi level parent relation search. - ๖ۣۜDiViN3 - Mar 30, 2018 PHP Code Usage: PHP Code RE: [Entity Framework] Multi level parent relation search. - severos - Mar 30, 2018 (Mar 30, 2018 05:06 PM)๖ۣۜDiViN3 Wrote: How is that related to my question?? RE: [Entity Framework] Multi level parent relation search. - ๖ۣۜDiViN3 - Mar 30, 2018 (Mar 30, 2018 09:32 PM)severos Wrote: How is that related to my question?? Retrieves all the property info from the selected category. RE: [Entity Framework] Multi level parent relation search. - severos - Mar 30, 2018 (Mar 30, 2018 10:05 PM)๖ۣۜDiViN3 Wrote: Retrieves all the property info from the selected category. Who ever said anything about retrieving property info?? Have you even used Entity Framework before? because you seem to not understand what I'm looking for here. RE: [Entity Framework] Multi level parent relation search. - maype - Apr 1, 2018 Maybe, the best way to understand what you are doing is to learn SQL. Beside using class relation & some special method to achieve to extract you data. Entity Framework just manage SQL for you. Good luck ! RE: [Entity Framework] Multi level parent relation search. - severos - Apr 1, 2018 (Apr 1, 2018 12:01 PM)maype Wrote: Maybe, the best way to understand what you are doing is to learn SQL. Beside using class relation & some special method to achieve to extract you data. Entity Framework just manage SQL for you. I already know basic SQL, but writing a stored procedure to do that search (although it might have slightly better performance) will look ugly as well, it's just moving loop ugliness from my code to the server. RE: [Entity Framework] Multi level parent relation search. - maype - Apr 1, 2018 From what I understand from your question, you want to join the Sub Categories from the found categories right ?? I found this on stackoverflow. Maybe it can help. Sorry if I dont get ur needs. See: https://stackoverflow.com/questions/5010110/entityframework-join-using-join-method-and-lambdas Code var query = db.Categories // sourceRE: [Entity Framework] Multi level parent relation search. - Sakuto - Apr 1, 2018 My point would be to: - First retrieve every categories related to a parent category (letting EF doing with lazy loading, or just use Include()) - Second, retrieve every article which category is in the retrieved list (.Where(a => ids.Contains(a.Id))) Two request, but I don't see something simpler atm. RE: [Entity Framework] Multi level parent relation search. - severos - Apr 1, 2018 (Apr 1, 2018 12:21 PM)maype Wrote: From what I understand from your question, you want to join the Sub Categories from the found categories right ??No, I'm not looking to join tables at all... (Apr 1, 2018 12:46 PM)Sakuto Wrote: My point would be to: That's actually 2 requests if I have single nesting level, consider something like this: -Master Category -Parent1 -Parent2 -Child2-1 -Parent3 -Child3-1 If I'm searching using Parent3 id then your method works in 2 requests. However, if I'm searching using Parent1, first query will retrieve Parent2 and Parent3 ids, then I'll have to query each of them to get lowest level children ids, then query products with the list of IDs. RE: [Entity Framework] Multi level parent relation search. - Ninja - Apr 2, 2018 (Apr 1, 2018 12:09 PM)severos Wrote: it's just moving loop ugliness from my code to the server. Isnt that the whole point of abstraction? To move "ugliness" somewhere else? If I could use a stored procedure, no matter how ugly, I would. It is cleaner on the client side. RE: [Entity Framework] Multi level parent relation search. - severos - Apr 2, 2018 (Apr 2, 2018 07:17 AM)Ninja Wrote: Isnt that the whole point of abstraction? To move "ugliness" somewhere else? If I could use a stored procedure, no matter how ugly, I would. It is cleaner on the client side. I thought abstraction splits ugliness around to tiny bits that you no longer see them? I'll probably end up using stored procedures, just wanted to see if there are other alternatives around. RE: [Entity Framework] Multi level parent relation search. - Sakuto - Apr 2, 2018 https://stackoverflow.com/questions/11565423/most-efficient-method-of-self-referencing-tree-using-entity-framework Taking profit of EF. |