On my website I am trying to return a list which shows the users that the current user is following but I keep on recieving the error Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'
Any help would be greatful
User Controller
public ActionResult Index()
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindById(User.Identity.GetUserId());
return View(new Followuser()
{
User1ID = db.Followusers.Where(u => u.User1ID == currentUser.Id).ToList()
});
}
Followuser model
public class Followuser
{
[Key, Column(Order = 0)]
public string User1ID { get; set; }
[Key, Column(Order = 1)]
public string User2ID { get; set; }
}
You're calling ToList
for no obvious reason. It would also make things clearer if you moved the query out of the return statement. Breaking your code down, you've effectively got:
// We're not certain what type `db.Followusers` is, to be honest.
List<Foo> list = db.Followusers.Where(u => u.User1ID == currentUser.Id)
.ToList();
Followuser user = new Followuser() { User1ID = list };
return View(user);
Do you see how that middle statement doesn't make sense?
I suspect you just want the following, assuming that db.Followusers
is really something like IQueryable<Followuser>
:
// Single checks that there's exactly one record matching the predicate.
Followuser user = db.Followusers.Single(u => u.User1ID == currentUser.Id);
return View(user);
Or given that it's now fairly short and simple:
return View(db.Followusers.Single(u => u.User1ID == currentUser.Id));
See more on this question at Stackoverflow