Best Practices for Retrieving Data Using Entity Framework 4.0

I am trying to understand some fundamental best practices using the Entity Framework.

My EDM project has Group and User objects that can contain groups and users.

The question arises:

What is the best way to get users out of a group?

To simplify groups, simply create a context object and create a list from the group table.

But when I want to see users inside the group, the context closes (as it should be).

I thought of two approaches:

1) sending the group back, binding it to the context and using the Load () method for users and returning the list of users.

Here I do not know when to join, and when I should not and when EDM will grow, I will have to do a lot back and forth for each download link

2) linq request from the user side.
  from u in context. Users where u.Groups.Contains (group) choose u

Here I get an exception that only primitive types can use.

So what is the right way to do this?

Thanks Ronny

+3
source share
2 answers

, "--" "--" . , a group may contain users - . : from u in context.Users where u.Groups.Contains(group) select u - --. , .

. . :

context.Users.Where(u => u.Group.Id == group.Id);

. . :

context.Users.Where(u => u.Groups.Any(g => g.Id == group.Id));
+3
context.Users.Select(u => u).Where(e => e.Groups.Contains(group))

?

0

Source: https://habr.com/ru/post/1754307/


All Articles