【问题标题】:LINQ Query which is using a query in where clause在 where 子句中使用查询的 LINQ 查询
【发布时间】:2018-08-14 18:27:02
【问题描述】:

我想实现以下场景...

表 1 - 数据库 1 的 tAccessRequest 列 - RequestId、CardNo 等...... 表 2 - 数据库 2 的 tBadge 列 - CardNo、CardStatus 等...

我创建了一个如下的请求类

public class RequestDetails
{
    public int RequestID { get; set; }

    public int RequestTypeID { get; set; }

    public string PersonID { get; set; }

    public int SectionCode { get; set; }

    public int RequestStateID { get; set; }

    public int ApprovalStatusID { get; set; }
}

现在我正在编写两个 LINQ 查询

List< RequestDetails > listReq = new List< RequestDetails >();

listReq = (from PP in DB1.tAccessRequests
                               where (PP.RequestStateID == 1 || PP.ApprovalStatusID == 1) && PP.SectionCode != null
                               select new RequestDetails
                               {
                                   RequestID = PP.RequestID,
                                   SectionCode = PP.SectionCode.Value 
                               }).ToList();

在我要实现的第二个LINQ查询中

var CardNoList = (from BC in prowatchContext.BADGE_C 
                                               where BC.STAT_COD != 'A' && BC.CARDNO in ("Select SectionCodefrom listReq"))

如何编写第二个 LINQ 查询..

请帮忙

【问题讨论】:

  • 签入 Linq 内部联接 stackoverflow.com/questions/37324/…
  • 感谢 Kevin 的更新,但是当我访问两个不同的数据库时,连接条件失败了。
  • what is your cardNo type ?
  • 抱歉忘了说,RequestDetail类中的sectionCode是cardNo

标签: .net sql-server asp.net-mvc linq c#-4.0


【解决方案1】:

您只需将Contains 用于IN

var CardNoList = (from BC in prowatchContext.BADGE_C 
                  where BC.STAT_COD != 'A' &&
                        listReq.Select(lr => lr.SectionCode).Contains(BC.CARDNO)
                  ).ToList();

注意:如果listReq 很大,这可能不起作用,除非您在客户端上进行Contains 测试,在这种情况下您可能需要使用HashSet

var reqCardNos = new HashSet<int>(listReq.Select(lr => lr.SectionCode));

var CardNoList = prowatchContext.BADGE_C.Where(BC => BC.STAT_COD != 'A')
                                        .AsEnumerable()
                                        .Where(BC => reqCardNos.Contains(BC.CARDNO))
                                        .ToList();

AsEnumerable 会将所有匹配的行拉到客户端然后过滤它们,所以如果BADGE_C 非常大,这也可能无法正常工作,在这种情况下您可能需要将reqCardNos 推送到prowatchContext数据库在一个临时表中然后做一个join。

【讨论】:

    【解决方案2】:

    试试这个:

    var CardNoList = from BC in prowatchContext.BADGE_C 
                                                  join lr in listReq on BC.CARDNO equals lr.CardNo where BC.STAT_COD != 'A'
                                                  select BC
    

    【讨论】:

    • lr.CardNo 你是怎么得到的?
    • 我试过这个,但我收到以下错误; “此上下文仅支持原始类型或枚举类型。”
    • var CardNoList = from BC in prowatchContext.BADGE_C join lr in listReq on BC.CARDNO 等于 lr.sectionCode where BC.STAT_COD != 'A' select BC
    • 嗨凯文,我试过这个,但我得到了上述错误。连接在这里不起作用,因为我们正在连接一个类对象和一个实体对象
    • 您可能需要将 BC.CARDNO 转换为 int:..join lr in listReq on int.parse(BC.CARDNO)..
    猜你喜欢
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多