首页 > 解决方案 > LINQ 使用不同数据类型的连接

问题描述

我在两个表约会和服务之间有关系,当 Id(Int) 等于约会表中的服务列时,我想从服务表中查看名称

public class Appointments
{
    public virtual Services Service { get; set; }
}

public class UsersViewModels
{
    public Services services { get; set; }
    public Appointments appointments { get; set; }
}

控制器:

// GET
public ActionResult Index( Services services, Appointments appointments)
{
  var username = (from emp in db.Appointments
                    join w in db.Services on emp.Service equals w.Id 
                    select new UsersViewModels
                    {
                        appointments = emp,
                        services= w.UserName,
                    });
 return View(username);
}

看法:

@item.appointments.Service.Name

如何将服务类型转换为 Int?

标签: asp.netasp.net-mvc-5

解决方案


我想你可以使用下面的代码,

与之比较emp.Service.Id equals w.Id

var username = (from emp in db.Appointments
                        join w in db.Services on emp.Service.Id equals w.Id
                        select new UsersViewModels
                        {
                            appointments = emp,
                            services = w.UserName,
                        });

推荐阅读