I am using the following code to allow for users to search for other registered users in order to add them to a friends list:
[Authorize(Roles = "RegisteredUsers")]
public ActionResult Index(string searchString)
{
//list of registered users
var regUsers = db.Users;
if (!String.IsNullOrEmpty(searchString))
{
regUsers = regUsers.Where(s => s.User.LastName.Contains(searchString));
}
return View(regUsers.ToList());
}
Yet I am getting an error on line 9 at regUsers = regUsers.Where(..)
, stating:
"cannot implicitly convert type
System.Linq.IQueryable<ETLToolKit.Models.User>
toSystem.Data.entity.DbSet<ETLToolKit.Models.User>
How can I reuse the regUsers
variable?
How about
IQueryable<User> regUsers = db.Users;
db.Users is of Type DbSet, so when you do your Where
call, it tries to implicitly assign a value of different type to regUsers
, which doesn't work - hence the error.
EDIT: As others have pointed out, the call to AsQueryable
can be omitted. regUsers is now explicitly of type IQueryable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With