Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 正在where子句中使用查询的LINQ查询_.net_Sql Server_Asp.net Mvc_Linq_C# 4.0 - Fatal编程技术网

.net 正在where子句中使用查询的LINQ查询

.net 正在where子句中使用查询的LINQ查询,.net,sql-server,asp.net-mvc,linq,c#-4.0,.net,Sql Server,Asp.net Mvc,Linq,C# 4.0,我想实现下面的场景 表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

我想实现下面的场景

表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 
                                              join lr in listReq on BC.CARDNO equals lr.CardNo where BC.STAT_COD != 'A'
                                              select BC

您只需对中的
使用
包含

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();
var reqCardNos=newhashset(listReq.Select(lr=>lr.SectionCode));
var CardNoList=prowatchContext.BADGE\u C.Where(BC=>BC.STAT\u COD!=“A”)
.可计算的()
其中(BC=>ReqCardNo.Contains(BC.CARDNO))
.ToList();

AsEnumerable
将把所有匹配的行拉到客户端,然后对它们进行过滤,因此如果
BADGE_C
非常大,这也可能无法正常工作,在这种情况下,您可能必须将
reqCardNos
推送到临时表中的
prowatchContext
数据库,然后执行联接。

检查Linq内部联接感谢Kevin的更新,但由于我正在访问两个不同的数据库,联接条件失败。您的cardNo类型是什么?抱歉,忘了提及,RequestDetail类中的sectionCode是cardNo
lr.cardNo
您是如何得到它的?我尝试了这个方法,但得到了以下错误;“此上下文中仅支持基元类型或枚举类型。”var CardNoList=from BC in prowatchContext.BADGE_C join lr in listReq on BC.CARDNO等于lr.sectionCode,其中BC.STAT_COD!='A‘选择BCHi kevin,我尝试了这个,但是我得到了上面的错误。连接在这里不起作用,因为我们正在连接一个类对象和一个实体对象。您可能需要将BC.CARDNO转换为int:…在int.parse(BC.CARDNO)上的listReq中连接lr。。
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();