Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
C# 使用linq与列表进行比较_C#_Linq - Fatal编程技术网

C# 使用linq与列表进行比较

C# 使用linq与列表进行比较,c#,linq,C#,Linq,我有一个列表cids,我就是这样抓取的 var cids = _service.Employee.Where(i => i.EmpID == _empID).Select(j => j.ClientID).ToList(); class Patient { Int PatientID{ get; set;} Int ClientID{get; set;} string PatientName{get; set;} } foreach

我有一个列表
cids
,我就是这样抓取的

var cids = _service.Employee.Where(i => i.EmpID == _empID).Select(j => j.ClientID).ToList();
class Patient   
{   
    Int PatientID{ get; set;}   
    Int ClientID{get; set;}   
    string PatientName{get; set;}
}
foreach(var item in cids)
{
     var pp = from p1 in _service.Patients
                            where p1.ClientId == item
                            select new PatientDTO
                            {
                                PatientID = p1.PatientID,
                                PatientName = p1.PatientName,

                            };
     prec.Add(pp);
}
我想将此列表与
患者实体
进行比较,并获取与
cid列表

患者实体是这样的

var cids = _service.Employee.Where(i => i.EmpID == _empID).Select(j => j.ClientID).ToList();
class Patient   
{   
    Int PatientID{ get; set;}   
    Int ClientID{get; set;}   
    string PatientName{get; set;}
}
foreach(var item in cids)
{
     var pp = from p1 in _service.Patients
                            where p1.ClientId == item
                            select new PatientDTO
                            {
                                PatientID = p1.PatientID,
                                PatientName = p1.PatientName,

                            };
     prec.Add(pp);
}
现在我就是这样做的

var cids = _service.Employee.Where(i => i.EmpID == _empID).Select(j => j.ClientID).ToList();
class Patient   
{   
    Int PatientID{ get; set;}   
    Int ClientID{get; set;}   
    string PatientName{get; set;}
}
foreach(var item in cids)
{
     var pp = from p1 in _service.Patients
                            where p1.ClientId == item
                            select new PatientDTO
                            {
                                PatientID = p1.PatientID,
                                PatientName = p1.PatientName,

                            };
     prec.Add(pp);
}
有没有一种方法可以使用
Linq
而不使用
foreach

获取公共记录

var commonClients = cids.Intersect<int>(_service.Patients.Select(x => x.ClientID));

var person = _service.Patients.Where(x => commonClients.Contains(x.ClientID));
var commonClients=cids.Intersect(_service.Patients.Select(x=>x.ClientID));
var person=_service.Patients.Where(x=>commonClients.Contains(x.ClientID));
用于获取常用记录

var commonClients = cids.Intersect<int>(_service.Patients.Select(x => x.ClientID));

var person = _service.Patients.Where(x => commonClients.Contains(x.ClientID));
var commonClients=cids.Intersect(_service.Patients.Select(x=>x.ClientID));
var person=_service.Patients.Where(x=>commonClients.Contains(x.ClientID));

您可以在列表中使用
Contains
(顺便说一句,您不需要
列表
,这样可以避免对数据库进行两次查询)

但在db世界中,最有效的方式是加入

from emp in _service.Employee.Where(i => i.EmpID == _empID)
join patient in _service.Patients on emp.ClientId equals patient.ClientId
select new PatientDTO {
  PatientID = patient.PatientID,
  PatientName = patient.PatientName,
}

您可以在列表中使用
Contains
(顺便说一句,您不需要
ToList
,这样可以避免对数据库进行两次查询)

但在db世界中,最有效的方式是加入

from emp in _service.Employee.Where(i => i.EmpID == _empID)
join patient in _service.Patients on emp.ClientId equals patient.ClientId
select new PatientDTO {
  PatientID = patient.PatientID,
  PatientName = patient.PatientName,
}

+1.我不明白为什么这个答案会被投反对票。这个答案直接响应了请求:
foreach
循环被重写为
Where
,其余的则保持不变。当然,
Intersect
会更好(通常,并非总是),但是,嘿,这个回答对实际问题有100%的准确性。@RaphaëlAlthaus谢谢,您的第一个查询非常有效。我在您的第二次查询中遇到此错误,
“join子句中某个表达式的类型不正确。调用join时类型推断失败”
是否可以help@ElectricRouge员工和患者中的ClientId类型是否相同?或者其中一个可以为空?@RaphaëlAlthaus在Employee中是它的
short
,在Employee中是
int
Patient@ElectricRouge如果执行
var pp=,就会得到这个结果;var result=pp.ToList()+1。我不明白为什么这个答案被投了反对票。这个答案直接响应了请求:
foreach
循环被重写为
Where
,其余的则保持不变。当然,
Intersect
会更好(通常,并非总是),但是,嘿,这个回答对实际问题有100%的准确性。@RaphaëlAlthaus谢谢,您的第一个查询非常有效。我在您的第二次查询中遇到此错误,
“join子句中某个表达式的类型不正确。调用join时类型推断失败”
是否可以help@ElectricRouge员工和患者中的ClientId类型是否相同?或者其中一个可以为空?@RaphaëlAlthaus在Employee中是它的
short
,在Employee中是
int
Patient@ElectricRouge如果执行
var pp=,就会得到这个结果;var result=pp.ToList()首选提供示例而非链接。首选提供示例而非链接。