Linq to SQL query SubQuery

本文关键字:SubQuery query SQL to Linq | 更新日期: 2023-09-27 18:22:05

我有一个关于我在这里问的问题的后续问题

这是我所有的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",而是想将查询1的结果传递给查询2,并在dataGrid中显示查询2的结果。我该怎么做?

Linq to SQL query SubQuery

类似的东西?

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方法检查检索到的RC代码是否是F0005表中的代码之一。

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
                      };