MVC - 使用LINQ / lambda连接多个表 - 使用EF

时间:2016-01-28 14:56:28

标签: c# asp.net-mvc entity-framework linq

我正在实施一个控制器,我需要让所有具有特定RiskTypeID的员工在用户点击导航项时选择。

以下是我在SQL中创建连接的方法

SQL

Select 
RTHG.RiskTypeID,
SM.FullName

From RiskTypeHasGroup RTHG
Inner join RiskGroup RG On RTHG.RiskGroupID = RG.ID
Inner join RiskGroupHasGroupMembers RGHGM ON RG.ID = RGHGM.RiskGroupID
Inner Join GroupMember GM ON RGHGM.GroupMemberID = GM.ID
Inner Join GroupMemberHasStaffMember GMHSM ON GM.ID = GMHSM.GroupMemberID
Inner Join StaffMember SM ON GMHSM.StaffMemberID = SM.ID

Where RTHG.RiskTypeID = 1

我在使用Linq和lambda之前撤回了数据,但只使用了简单的表达式,我现在需要能够进行调用,这将带回与上面概述的sql相同的数据,我已经在线搜索了但是可以找不到与我的要求相似的东西。

这是我的控制器,我将评论作为指导

控制器

public ActionResult ViewRiskTypes(int SelectedRiskTypeID)
{
    var RiskTypes = _DBContext.RiskTypes.ToList(); // Get all of the current items held in RiskTypes tables, store them as a List in Var RiskTypes
    var ViewModel = new List<RiskTypeWithDetails>(); // Create colletion which holds instances of RiskTypeWithDetails and pass them to the ViewModel
    var Details = new RiskTypeWithDetails(); // Create a new instance of RiskType with details and store the instance in var Details

    foreach (var RiskType in RiskTypes) // Loop through each Item held in var RiskTypes
    {
        Details.RiskTypes.Add(new RiskTypesItem { ID = RiskType.ID, Description = RiskType.Description }); // assign each items ID & Description to the same feilds in a new
        // instance of RiskTypeItems (which is a property of RiskTypeWithDetails)
    }

    foreach (var RiskType in RiskTypes)  // Loop through each item in RiskTypes
    {
        if (RiskType.ID == SelectedRiskTypeID) // Check Item ID matches SelectedRiskTypeID value
        {
            //var Details = new RiskTypeWithDetails();
            Details.RiskTypeDescription = RiskType.Description;  //assign the Risk type Descripton to RiskTypeWithDetails RiskTypeDescription Property
            Details.RiskDetails = _DBContext
                .RiskTypeHasGroups
                //.GroupMemberTypeHasGroupMembers
                .Where(r => r.RiskTypeID == SelectedRiskTypeID) // Where RiskTypeId matches Selected ID bring back following data from Db
                .Select(r => new RiskDetails
                {
                    RiskGroupDescription = r.RiskGroup.Description,
                    GroupMembers = r.RiskGroup.RiskGroupHasGroupMembers
                            .Select(v => v.GroupMember).ToList(),
                    //StaffMembers = r.RiskGroup.RiskTypeHasGroups
                    //              .Join(r.RiskGroup.RiskTypeHasGroups,
                    //              a => a.RiskGroupID , b => b.RiskGroup.ID,
                    //              (a, b) => new {a, b})
                    //              .Join(r.RiskGroup.RiskGroupHasGroupMembers,
                    //              c => c.) // Dosent join as I would expect... no idea what to do here
                }).ToList();

            ViewModel.Add(Details); //Add all data retrieved to the ViewModel (This creates one item in the collection)
        }
    }
    return View(ViewModel);
}

正如您将看到的,我想让所有职员与所选的RiskTypeID匹配。我需要一些帮助来将上面的SQL转换为我的控制器中的lambda表达式

提前致谢

2 个答案:

答案 0 :(得分:0)

您使用已注释的代码走在正确的轨道上!对于初学者,LINQ有两个不同的sytax:querymethod chain。您使用的是method chain语法,它很快就会变得无法维护。

对于像这样的实例,query语法就是它所在的位置。

结果如下:

from rhtg in _dbContext.RiskTypeHasGroup

where rhtg.RiskTypeID == 1

join rg in _dbContext.RiskGroup 
  on rhtg.RiskGroupID equals rg.ID

join rghgm in _dbContext.RiskGroupHasGroupMembers 
  on rg.ID equals rhtg.ID

join gm in _dbContext.GroupMember
  on rg.ID equals gm.ID

join gmhsm in _dbContext.GroupMemberHasStaffMember
  on gm.ID equals gmhsm.GroupMemberID

join sm in _dbContext.StaffMember 
  on gmhsm.StaffMemberID equals sm.ID

select new 
{
  rhtg.RiskTypeId,
  sm.FullName 
};

请注意,我使用.Net约定来表示不同的变量。

以下是query语法的一些文档: https://msdn.microsoft.com/en-us/library/gg509017.aspx

答案 1 :(得分:-1)

您可以在linq中编写完全相同的查询,如下所示:

var query = (from RTHG in _DBContext.RiskTypeHasGroup RTHG
            join RG in _DBContext.RiskGroup on RTHG.RiskGroupID equals RG.ID
            join RGHGM in _DBContext.RiskGroupHasGroupMembers on RG.ID equals RGHGM.RiskGroupID
            join GM in _DBContext.GroupMember on RGHGM.GroupMemberID = GM.ID
            join GMHSM in _DBContext.GroupMemberHasStaffMember on GM.ID equals GMHSM.GroupMemberID
            join SM in _DBContext.StaffMember on GMHSM.StaffMemberID equals SM.ID
        where RTHG.RiskTypeID == 1
        select new {RTHG.RiskTypeID,SM.FullName});