Linq多条件字段的多表连接

假设,我们有一个类StationCat,它的具体属性如下:

public class StationCat
{
    public string CustomerModel { get; set; }

    public string baj_cZZLINE { get; set; }

    public string IssuePosition { get; set; }

    public string fact { get; set; }

    public int InputCount { get; set; }

    public int RepCount { get; set; }

    public string S_NM { get; set; }
}

此时,我们有2个由这个类实例化出的实体数组SWAPMaterials和StationCats,当我们想要把SWAPMaterials左连接到StationCats时,条件为SWAPMaterials表中元素的CustomerModel、baj_cZZLINE、S_NM与StationCats表元素对应的属性相等,那我们应该按照如下方式写LINQ

var resSWAPMaterials = (from p in SWAPMaterials
join StationCat in StationCats on new { p.CustomerModel, p.baj_cZZLINE,p.S_NM } equals new { StationCat.CustomerModel,StationCat.baj_cZZLINE,StationCat.S_NM } into p_StationCat
from StationCat_join in p_StationCat.DefaultIfEmpty()
select new StationCat()
{
    CustomerModel = p.CustomerModel,
    baj_cZZLINE = p.baj_cZZLINE,
    IssuePosition = p.IssuePosition,
    fact = p.fact,
    RepCount = p.RepCount,
    S_NM = p.S_NM,
    InputCount = StationCat_join == null ? 0 : StationCat_join.InputCount
}).ToList();

到此,我们就实现了Linq多条件字段的多表连接

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注