Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello everyone.

am trying to include navigation properties that meet a specific condition but the include method doesn't support that.so how can i solve this?

the code i wish it to work is this:

C#
ctx.GetAll().include(o=>o.NavProp.Where(p=>p.Id==someId);


using Projection return anonymous types so i had to create a stongly defined type to encapsulate those returned items and then populate my TreeView with a list of this new type.this works fine but it require a lot of work because when i want to save new entities i have allways to create a new entity and fill it's properties with my new type properties.

I love to know if i can do the first approach with include method.

thank you for your time.

i hope it's clear.
Posted
Updated 21-Apr-20 2:59am
v2

Try this out:

C#
ctx.GetAll().Include(o=>o.NavProp).Where(p=>p.NavProp.Id==someId);
 
Share this answer
 
v5
Comments
tux@ddictor 6-Aug-13 17:59pm    
thank for the answer but what you suggest use the Where methode to get filter the parents and that's not what i want.i want to filter the childs (Navigation properties in term Of Entity Framework).So what i wish is something like this:

ctx.GetAll().Include(o=>o.NavProp.Where(navp=>navp.Id==someId);

:)
Silvabolt 7-Aug-13 10:06am    
Sorry man, I misunderstood, but I think my updated answer should work. Try it out :)

Note: What you are trying to do does not work because NavProp isn't an IEnumerable type, meaning you cannot attach a linq Where clause to the NavProp property itself. My current answer will grab you the parent objects that contain NavProp, that have their NavProp.Id equal to someId.
tux@ddictor 8-Aug-13 3:08am    
hi man i really Respect your help but let reexplain what i need.

let's say i have a table of parent and another table of childs,every parent may have multiple child but a child can have one parent,so that's a one to many relation,and that means that in my Genarated models a parent have a Navigation property Childs wich is a collection of child .
when i use this code:
ctx=GetAll.Include(o.Childs) all the parents are loaded and all their childs.so far so not Good :).why?

let say i want all the parents to be loaded but for every parent i want to load only his childs that for example has a First name started with B so what i may wich is something like this:

ListParents= ctx.GetAll().Include((p=>p.Childs).Where(c=>c.Name.StartsWith("B"));

i read some articles that says it's not possible to do that,becaus the include Methode doesn't accept filtering inside of it,it only accept the names of the properties to include no more no less.and also because Navpop are not IEnumerables as you mentioned.
i considered using projection wich allows that but i should always encapsulate the returned result in a defined type because the results are returned as an anonymous type wich can't allow me to populate my ObservableCollection<t> wich needs an IEnumerable or List to fill it.

projection works fine but it adds a lot of work and you now ,as programmers we hate that ,we lazy (well am lazy).

thanks again and i hope it's well explained :)
Silvabolt 8-Aug-13 9:41am    
I see what you mean now. I guess short answer is you're right, you can't do that because Include() eager loads everything of the specified property.

Long answer is you can get around it by doing post filtering, i.e., perform filtering on "ListParents" after you grab the data.

This guy had the same issue as you, notice that the selected answer doesn't even use include:
http://stackoverflow.com/questions/11073727/filter-linq-entity-include-query

It should point you in the right direction, although it will be a bit more intricate for you because your NavProp property is a collection itself.
tux@ddictor 10-Aug-13 4:21am    
hi Silvabolt.thank you so much for your effort.yes Include() is eager but infortunatelly more then how i want it to be.

the first solution in the link you suggested is what i did actually usedbecause that's what names projection.projection use The Select Methode but unfortunatelly it retruns an Anonymous type so we can't use that to fill an observableCollection wich i need to bind to my treeview,but we have to create a strongely type that looks kinda the same as my parent model and inside it we can use the Where() to filter the child navprop(collection).

that's what i used and it works fine but it add more work because when i want to deal with tha Database i have always to grab the id in my new type and then get the original record who has that same id then manipilate it as i wish.

i highly appreciate your effort and i really thank you for that,that's nice from you.i wish u a happy life and a happy programming .
best of luck
var result = _context.ctx.GetAll();
_context.Entry(result).Collection(o => o.NavProp).Query().Where(p=> p.NavProp.Id == someId).tolist();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900