Linq to SQL-获取相关数据

时间:2018-06-19 18:01:45

标签: c# entity-framework linq-to-sql entity-framework-6

我有一些数据,我需要返回它的一些相关数据并将其全部放入模型中。我在模型中设置了所有适当的字段,看起来像这样:

.outer {
    position: relative;
}
.redbox {
    background: rgba(255, 0, 0, 1);
    height: 20px;
}
.bluebox {
    background: rgba(0, 0, 255, 1);
    height: 20px;
}
.myboxes {
    position: absolute;
    z-index: 2;
}
#cats {
    position: absolute;
    z-index:1;
}

这是我要使用的linq语句:

public class ComputerModel
{
    public int MachineId { get; set; }
    public int GroupId { get; set; }
    public int SoftwareVersionId { get; set; }
    public string GroupName { get; set; }
    public string SoftwareVersion { get; set; }
    public string IPAddress { get; set; }
    public string HostName { get; set; }
    public string MACAddress { get; set; }
    public string Title { get; set; }
    public bool IsIGMonitor { get; set; }
    public string UpTime { get; set; }
    public DateTime DateEntered { get; set; }
    public string EnteredBy { get; set; }
    public Nullable<DateTime> DateUpdated { get; set; }
    public string UpdatedBy { get; set; }
    public ICollection<MachineRole> MachineRoles { get; set; }
    public ICollection<Role> Roles { get; set; }
}

我可以让MachineRoles填充,但是我不能让Roles填充。我已经尝试过var query = (from m in unitOfWork.Context.Machines join u in unitOfWork.Context.Users on m.EnteredBy equals u.UserId into EntByUser from EnteredByUser in EntByUser.DefaultIfEmpty() join u2 in unitOfWork.Context.Users on m.UpdatedBy equals u2.UserId into UpdByUser from UpdatedByUser in UpdByUser.DefaultIfEmpty() join g in unitOfWork.Context.Groups on m.GroupId equals g.GroupId into Grp from Groups in Grp.DefaultIfEmpty() join s in unitOfWork.Context.SoftwareVersions on m.SoftwareVersionId equals s.SoftwareVersionId into SW from SoftwareVersions in SW.DefaultIfEmpty() join mr in unitOfWork.Context.MachineRoles on m.MachineId equals mr.MachineId into MachRoles from MachineRoles in MachRoles.DefaultIfEmpty() join r in unitOfWork.Context.Roles on MachineRoles.RoleId equals r.RoleId into Rolz from Rolz2 in Rolz.DefaultIfEmpty() select new ComputerModel() { MachineId = m.MachineId, GroupId = m.GroupId, SoftwareVersionId = m.SoftwareVersionId, GroupName = Groups.GroupName, SoftwareVersion = SoftwareVersions.Version, IPAddress = m.IPAddress, HostName = m.HostName, MACAddress = m.MACAddress, Title = m.Title, IsIGMonitor = m.IsIGMonitor, UpTime = m.UpTime, DateEntered = m.DateEntered, DateUpdated = m.DateUpdated, EnteredBy = EnteredByUser.FirstName + " " + EnteredByUser.LastName, UpdatedBy = UpdatedByUser.FirstName + " " + UpdatedByUser.LastName, MachineRoles = m.MachineRoles, Roles = ????? }).ToList(); ,但Rolz返回的是Role的单个实例,而不是集合。

如何获取此查询以返回MachineRoles和Roles的Machines和相关数据?

我看过以下文章,但运气不佳:
This SO Article
Loading Related Data - MSDN
Using Include with Entity Framework

更新
我注意到,如果删除模型并使用匿名类型,则不会出现任何错误:

Roles = Rolz2

但这对我的项目没有帮助,因为我需要对数据使用模型。

1 个答案:

答案 0 :(得分:0)

如果以上所有表都是通过PK-FK关系关联的,则可以使用linq lambda函数.include()来包含相关表,然后使用Navigation属性来访问数据。

如果表不相关,则可以使用LINQ左联接,如http://www.devcurry.com/2011/01/linq-left-join-example-in-c.html所示。

您似乎需要混合使用内部联接和左联接。上面的示例仅实现内部联接。