Linq to SQL查询SubQuery

时间:2013-07-11 19:19:40

标签: c# linq oracle

关于我问here

的问题,我有一个跟进问题

这是我所有的Linq查询

这是查询1:

var RCode = from a in DbEntity.MobileAssetDowntimeReasonCodes 
            where a.MobileAssetCategoryId.Equals(reasonCode) 
            select new 
            { 
                a.JdeReasonCode 
            };

ReasonCode.DataSource = RCode.ToList();
ReasonCode.DataBind();

这会带来一个显示在DataGrid中的结果代码。

这里是查询2也可以使用

var RJDEReasonCode = from a in JDETable.F0005
                                 where
                                   a.DRSY.Equals("00") &&
                                   a.DRDL01 != null &&
                                   (a.DRRT.Equals("W4") ||
                                   a.DRRT.Equals("W5")) &&
                                   a.DRKY.Trim() == "801"
                                 select new
                                 {
                                     CATEGORY_CODE = a.DRRT,
                                     REASON_CODE = a.DRKY,
                                     DESCRIPTION = a.DRDL01
                                 };

但不是硬编码" 801"我想将query1的结果传递给查询2并在我的dataGrid中显示查询2结果。我该怎么做?

2 个答案:

答案 0 :(得分:2)

这样的东西?

List<string> RCode = DbEntity.MobileAssetDowntimeReasonCodes
    .Where(a=>a.MobileAssetCategoryID.Equals(reasonCode))
    .Select(a=>a.JdeReasonCode).ToList();

var RJDEReasonCode = JDETable.F0005
    .Where(a=>a.DRSY.Equals("00") && a.DRDL01 != null &&
             (a.DRRT.Equals("W4") || a.DRRT.Equals("W5")) &&
              RCode.Any(code => code.Contains(a.DRKY.Trim())))
    .Select(a=>new { CATEGORY_CODE = a.DRRT,
                     REASON_CODE = a.DRKY,
                     DESCRIPTION = a.DRDL01
    });

使用Enumerable.Any Method检查检索到的RCodes是否是F0005表中的代码之一。

答案 1 :(得分:0)

var RJDEReasonCode = from a in JDETable.F0005
                     where
                         a.DRSY.Equals("00") &&
                         a.DRDL01 != null &&
                         (a.DRRT.Equals("W4") ||
                         a.DRRT.Equals("W5")) &&
                         //and use it checking if it contains a.DRKY.Trim() 
                         RCode.Contains(a.DRKY.Trim())
                      select new
                      {
                                     CATEGORY_CODE = a.DRRT,
                                     REASON_CODE = a.DRKY,
                                     DESCRIPTION = a.DRDL01
                      };