A
A
Alexey Maltsev2015-03-22 08:48:45
ASP.NET
Alexey Maltsev, 2015-03-22 08:48:45

How to load a collection of elements through a many-to-many relationship in Entity Framework?

Good day.
Now I am writing a program using Entity Framework 6 and I am binding data from the context to the DataGridView.
There are 3 entities: Wallet, WalletM2M, Account. Wallet and Account are related to WalletM2M by a many-to-many relationship. You need to get a list of Wallet records, knowing only the data about a particular Account record.
I do it like this:

var l = context.Wallet.Include(w => w.WalletM2M).Select(w => w.WalletM2M
               .Where(wM2M => wM2M.AccountId == CurrentUser.AccountId && wM2M.IsAdmin == true)).ToList();

But as a result I get a list of lists of Wallet records, with a dimension equal to the number of all Wallet records in the database
Wallet table
630e063195ff43eda0b9237fa25c6bc3.JPGReceived list
fc7a73d4fe8044d8b9b5e89983e125b4.JPGEntity diagram
40870d2a7b12421cb6f881d7444e2dee.JPG

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Puma Thailand, 2015-03-22
@Axel_User

If you want to get the wallet to the current userid, then just filter

context.Wallet.Include(w => w.WalletM2M)
               .Where(w => w.WalletM2M.AccountId == CurrentUser.AccountId && w.WalletM2M.IsAdmin == true).ToList();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question