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()代码>首选提供示例而非链接。首选提供示例而非链接。