简体   繁体   English

无法将类型'System.Collections.Generic.List'隐式转换为'string'

[英]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.

相关问题 无法隐式转换类型&#39;System.Collections.Generic.List <System.Data.DataRow> &#39;到&#39;System.Collections.Generic.List <string> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>' 无法隐式转换类型&#39;System.Collections.Generic.List &lt; <anonymous type> &gt;”到“ System.Collections.Generic.List <string> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type>>' to 'System.Collections.Generic.List<string>' C#:无法隐式转换类型“System.Collections.Generic.List”<char> &#39; 到 &#39;System.Collections.Generic.List<string> &#39; - C#: Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>' 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>' 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 无法隐式转换类型&#39;System.Collections.Generic.List错误 - Cannot implicitly convert type 'System.Collections.Generic.List error 无法隐式转换类型&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM