Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 无法隐式转换类型';System.Collections.Generic.List<;字符串>';至';System.Collections.Generic.List<;模型>;Mvc_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 无法隐式转换类型';System.Collections.Generic.List<;字符串>';至';System.Collections.Generic.List<;模型>;Mvc

Asp.net mvc 无法隐式转换类型';System.Collections.Generic.List<;字符串>';至';System.Collections.Generic.List<;模型>;Mvc,asp.net-mvc,Asp.net Mvc,我遇到问题,无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List” 型号: 您正在选择一个用户名列表,因此您将得到一个列表,并且您的属性是一个列表您的查询正在选择一个包含用户名的字符串列表。并且不能将字符串列表转换为FuelAppoints列表。您应更改为: public ActionResult Index() { var model = new UsersViewMod

我遇到问题,无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List” 型号:


您正在选择一个用户名列表,因此您将得到一个
列表
,并且您的属性是一个
列表

您的查询正在选择一个包含用户名的字符串列表。并且不能将字符串列表转换为FuelAppoints列表。您应更改为:

 public ActionResult Index()
    {
        var model = new UsersViewModels();
        model.appointmentslist = db.AspNetUsers.Where(
        user => db.FuelerAppointments.Any(f => f.AspNetUserId == user.Id))
       .Select(user => user.FuelerAppointments).ToList();

        return View(model);
    }

编译器不允许您这样做。您已将列表声明为
list
,无法将其分配给
String
。如果您在
fuelerappoition

我有两个相同型号的喇叭

 public class FuelerAppointment
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public int AspNetUserId { get; set; }
}

public class AspNetUser
{
    public string UserName { get; set; }
    public int Id { get; set; }
}
和视图模型:

public class UsersViewModels
{
    public List<FuelerAppointment> appointmentslist { get; set; }
}
结果:


这可能会有所帮助:我尝试过,但出现错误:System.NotSupportedException:“实体或复杂类型”Startupfuels.Data.FuelerAppoition“无法在LINQ to Entities查询中构造。”(仍然相同,但我可以做到。)
 public class FuelerAppointment
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public int AspNetUserId { get; set; }
}

public class AspNetUser
{
    public string UserName { get; set; }
    public int Id { get; set; }
}
public class UsersViewModels
{
    public List<FuelerAppointment> appointmentslist { get; set; }
}
 [HttpGet]
    public UsersViewModels Get()
    {
        var model = new UsersViewModels
        {
            appointmentslist = (from user in _dbContext.AspNetUsers
                join fuelerAppointment in _dbContext.FuelerAppointments on user.Id equals fuelerAppointment.AspNetUserId
                select new FuelerAppointment { UserName = user.UserName }).ToList()
        };

        return model;
    }