user1615362
user1615362

Reputation: 3807

How to return List instead of an object using Lambda and EF

I have this code that returns a single user:

  return RetryWithExpression<User, User>(u => u.FirstOrDefault(x => x.UserEmail == userEmail));

I am trying to convert it so that It returns many users like this:

return RetryWithExpression<User, List<User>>(u => u.Select(x => x.sUserCity == cityId));

This does not compile and I get an error:

Cannot implicitly convert type 'System.Linq.IQueryable<bool>' to 'System.Collections.Generic.List<User>'. An explicit conversion exists (are you missing a cast?)

How do I return a List from this method?

Upvotes: 1

Views: 164

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190925

I think you want Where which filters. Select does a projection. In your case Select would return an IEnumerable<bool>, hence the compilation error.

return RetryWithExpression<User, List<User>>(u => u.Where(x => x.sUserCity == cityId));

Since RetryWithExpression expects a list, call ToList()

return RetryWithExpression<User, List<User>>(u => u.Where(x => x.sUserCity == cityId).ToList());

Upvotes: 4

Related Questions