Reputation: 439
I have a function that returns an entity framework table, but i only need certain id's which are passed in via another entity list. I'm not sure the correct way to do this, i've tried the following but the list is causing issues...
public IEnumerable<Payment> PaymentsForContact(List<User> user) =>
_context.Payments.Where(p=> p.Id == user.Id;
Is there a way i can iterate through user object to only return the id's in user that are in _context.Payments?
Thanks in advance
Upvotes: 0
Views: 2166
Reputation: 84
I think this code do what you want:
_context.Payments.Where(p=> user.Any(u=> u.Id == p.Id));
For each payment you have to check if user list contains that payment
Upvotes: 2
Reputation: 2654
To iterate through something, that is in two tables, you use a join
Payments.Join(Users, p=>p.UserID, u =>u.UserID, (p,u)=>p)
returns payments, that have a user.
If you already have a list of ID's than you can use Contains
var list = new List<int> { 1,2,3,4};
Payments.Where( p => list.Contains(p.UserId));
But this is only recommended for small lists of id (less than 100), cause all the id's become part of your query string.
Upvotes: 6