[英]Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'
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'
在我的网站上,我试图返回一个列表,该列表向用户显示当前用户正在关注的用户,但我一直收到错误消息:
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. 您没有明显的原因在呼叫
ToList
。 It would also make things clearer if you moved the query out of the return statement. 如果将查询从return语句中移出,这也将使事情更加清晰。 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>
: 我怀疑您只是想要以下内容,假设
db.Followusers
确实类似于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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.