0

I am having struggles finding the best method to implement an IN statement.

Currently I am using the below code in my controller to return a list of stances specific an individual customer account.

return _context.Stances
               .ToList()
               .Select(Mapper.Map<Stances, StancesDto>)
               .Where(c => c.AccountGUID == userAccountID.CustomerGUID);

I am in the process of creating a partner portal that would allow a Partner user to have access to many other customer accounts that each individual customer provides them access to.

I setup a partnerLink table that stores a PartnerGUID and a CustomerGUID, marrying the relationship. The struggle I have is finding a method to allow a one to many relationship using an "IN" or "contains" option.

What I am looking to do is something like this:

either load the list if it is just a customer "OR" if it is a partner account Load all customer stances in the partnerLink table.

 var partners = _context.PartnerLinks
                        .Where(user => user.PartnerGUID == userAccountID.CustomerGUID)
                        .Select(user => user.AccountGUID) // extract the emails from users
                        .ToList();

  return _context.Stances
                 .ToList()
                 .Select(Mapper.Map<Stances, StancesDto>)
                 .Where(c => c.AccountGUID == userAccountID.CustomerGUID || partners.Contains(c.AccountGUID));
8
  • 1
    Do you know that you have loaded whole table into the memory? Commented Jul 30, 2021 at 18:08
  • No I am not aware of that I am loading whole table. What part is that? Commented Jul 30, 2021 at 18:12
  • Where you have ToList that immediately loads an entire table into memory. Also, you should map to a DTO after you have filtered what you want to retrieve. Generally, Select should be the last thing you do. Commented Jul 30, 2021 at 18:12
  • Do you think you can try to re-write what you want to accomplish more clearly? Commented Jul 30, 2021 at 18:13
  • Thank you, I will make sure to remove that in other areas as well Commented Jul 30, 2021 at 18:13

2 Answers 2

2

You should not use ToList when combining LINQ queries. Everything should be IQueryable.

var partners = _context.PartnerLinks
    .Where(user => user.PartnerGUID == userAccountID.CustomerGUID)
    .Select(user => user.AccountGUID);

return _context.Stances
    .Where(c => c.AccountGUID == userAccountID.CustomerGUID || partners.Contains(c.AccountGUID))
    .AsEnumerable()
    .Select(Mapper.Map<Stances, StancesDto>);

Also consider to use Automapper's ProjectTo

return _context.Stances
    .Where(c => c.AccountGUID == userAccountID.CustomerGUID || partners.Contains(c.AccountGUID))
    .ProjectTo<StancesDto>(configuration);
Sign up to request clarification or add additional context in comments.

Comments

0

I would think you could do it with a Union.

Sometime like:

return _context.Stances.Select(Mapper.Map<Stances, StancesDto>).Where(c => c.AccountGUID == userAccountID.CustomerGUID)
.Union(_context.Stances.Select(Mapper.Map<Stances, StancesDto>).Where(c => partners.Contains(c.AccountGUID)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.