内部加入多个列表

时间:2019-10-14 22:28:04

标签: asp.net-mvc list linq

我对多重列表的内部联接有一些疑问。 首先,我有一个称为AdminSettingsModel的ViewModel

    public partial class AdminSettingsModal
{
   public List<ReportTypes> reportTypes { get; set; }
   public List<UserType> userType { get; set; }     

}

    public partial class ReportTypes
{
    public int Id { get; set; }
    public string ReportName { get; set; }
    public int? UserType { get; set; }
    public string IconPath { get; set; }
    public string ModuleName { get; set; }
    public int GotSop { get; set; }
    public int ThirdParty { get; set; }
}

    public partial class UserType
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string Access { get; set; }
}

并且我需要在控制器中返回userType和Reporttype(需要与userType进行内部联接)。

        public async Task<IActionResult> Index()
    {
        AdminSettingsModal admin = new AdminSettingsModal();
        admin.userType = await _context.UserType.ToListAsync();
        admin.reportTypes = await _context.ReportTypes.ToListAsync();

        return View(admin);
    }
  1. 任何想法,或者如果要使用内部联接方法(例如,sql如此),该怎么办

选择reportTypes.Id,reportTypes.ReportName,userType.Type FROM reportTypes INNER JOIN userType 开启reportTypes.UserType = userType.Id;

谢谢

1 个答案:

答案 0 :(得分:0)

  

您的SQL查询

SELECT reportTypes.Id, reportTypes.ReportName, userType.Type FROM reportTypes 
INNER JOIN userType ON reportTypes.UserType = userType.Id;
  

为报告创建一个模型类

public class ReportModel
{
    public int Id {get;set;}
    public string ReportName {get;set;}
    public int Type {get;set;}
}
  

内部联接查询

var oJoinData = from userType in _context.UserType 
JOIN ReportTypes in _context.ReportTypes ON
userType.Id = ReportTypes.UserType
select new ReportModel 
{
    Id = userType.Id,ReportName=ReportTypes.ReportName,Type = reportTypes.UserType
}