1

我正在使用 LINQ 加入两个表以匹配模型类,如下所示

在此处输入图像描述

    lstOptInInterest = new LinkedList<OptInInterestArea>
                       ((from a in dbEntities.SUBCODE
                       from appCode in dbEntities.CODE.Where(
                          x => x.CODE == a.CODE  && x.TYPE == a.TYPE)
                        select new OptInInterestArea()
                        {
                               Code = a.CODE,
                               SubCode = a.SUBCODE,
                               SubCodeDescription = a.DESCR,
                               CodeDescription = appCode.DESCR
                          }).ToList());

模型类

public class OptInInterestArea
{      
        [DisplayName("Code")]
        public string Code { get; set; }

        [DisplayName("Sub Code")]
        public string SubCode { get; set; }

        DisplayName("Sub Code Description")]
        public string SubCodeDescription { get; set; }

        [DisplayName("Code Description")]
        public string CodeDescription { get; set; }

        [DisplayName("Previous OptIn")]
        public bool PrevOptIn { get; set; }
}

表 B

在此处输入图像描述

现在我的问题是我需要从表 B 中分配PrevOptInlstOptInInterest (见上文)。表 B 可能包含也可能不包含所有CODESUBCODElstOptInInterest

if CODEand SUBCODEoflstOptInInterest存在于表 B 上,赋值给PrevOptInin elsePrevOptIn = N

我怎样才能做 LINQ 来得到这个?

4

1 回答 1

1

首先,我建议您使用INNER JOIN而不是CROSS JOIN查询您的 lstOptInInterest :

lstOptInInterest = new LinkedList<OptInInterestArea>
                       ((from a in dbEntities.SUBCODE
                         join appCode in dbEntities.CODE
                         on new {CODE=a.CODE,TYPE=a.TYPE} equals new {CODE=x.CODE,TYPE=x.TYPE}
                         select new OptInInterestArea()
                          {
                               Code = a.CODE,
                               SubCode = a.SUBCODE,
                               SubCodeDescription = a.DESCR,
                               CodeDescription = appCode.DESCR
                          }).ToList());

其次,LEFT OUT JOIN用于从表 B 中分配 lstOptInInterest 的 PrevOptIn 值:

lstOptInInterest = new LinkedList<OptInInterestArea>
            (
                  (from a in lstOptInInterest
                          join b in dbEntities.TableB
                          on new {CODE=a.Code,SUBCODE=a.SubCode} equals new {CODE=b.CODE,SUBCODE=b.SUBCODE}
                          into leftGroup
                          from b in leftGroup.DefaultIfEmpty()
                          select new OptInInterestArea()
                          {
                               Code = a.Code,
                               SubCode = a.SubCode,
                               SubCodeDescription = a.SubCodeDescription,
                               CodeDescription = a.CodeDescription,
                               PrevOptIn=b==null? false : b.OPTIN=="Y"
                          }).ToList()
            );

更新: 尝试仅通过 1 个步骤测试即可获得结果:

lstOptInInterest = new LinkedList<OptInInterestArea>
                       ((from a in dbEntities.SUBCODE
                         join appCode in dbEntities.CODE
                         on new {CODE=a.CODE,TYPE=a.TYPE} equals new {CODE=x.CODE,TYPE=x.TYPE}
                         join b in dbEntities.TableB
                         on new {CODE=a.CODE,SUBCODE=a.SUBCODE} equals new {CODE=b.CODE,SUBCODE=b.SUBCODE}
                         into leftGroup
                         from b in leftGroup.DefaultIfEmpty()
                         select new OptInInterestArea()
                          {
                               Code = a.CODE,
                               SubCode = a.SUBCODE,
                               SubCodeDescription = a.DESCR,
                               CodeDescription = appCode.DESCR,
                               PrevOptIn=b==null?false : b.OPTIN=="Y"
                          }).ToList());
于 2013-07-03T01:38:41.727 回答