我有两个数据表,我有一个过滤匹配记录的功能。以下是我的功能。
public static DataTable FilterCandidates(DataTable FullCandidateData, DataTable RequiredCandidateIDs)
{
DataTable ReqCandidateData = new DataTable();
try
{
var MacthingIDs = FullCandidateData.AsEnumerable().Select(r => r.Field<string>("Candidate_ID")).Intersect(RequiredCandidateIDs.AsEnumerable().Select(r => r.Field<string>("Candidate_ID")));
ReqCandidateData = (from row in FullCandidateData.AsEnumerable()
join Candidate_ID in MacthingIDs
on row.Field<string>("Candidate_ID") equals Candidate_ID
select row).CopyToDataTable();
return ReqCandidateData;
}
catch (Exception ex)
{
ReqCandidateData = FullCandidateData.Clone();
return ReqCandidateData;
}
}
有没有其他有效的方法可以做到这一点,不知怎的,我不习惯假设异常相当于没有匹配的记录。我也可以通过查找长度来做到这一点,但是它会大大增加no.of循环,我的First Datatable中有大约90,000个候选者,并且它会不断增长。
另外,如果我有多列比较,请告诉我如何执行此操作,例如 Candidate_ID 和 Job_ID
答案 0 :(得分:0)
您可以使用单个联接来为您的目的服务。下面的第一个函数只匹配Candidate_ID,第二个函数匹配Candidate和job id。请参阅第三种方法以获取&#34; FullCandidateData&#34;中的所有行。不符合&#34; RequiredCandidateIDs&#34;
public static System.Data.DataTable FilterCandidates(System.Data.DataTable FullCandidateData, System.Data.DataTable RequiredCandidateIDs)
{
System.Data.DataTable reqCandidateData = new System.Data.DataTable();
try
{
var reqCandidateDt = (from candidateRow in FullCandidateData.AsEnumerable()
join reqCandidateRow in RequiredCandidateIDs.AsEnumerable()
on candidateRow["Candidate_ID"].ToString() equals reqCandidateRow["Candidate_ID"].ToString()
select candidateRow);
if (reqCandidateDt.Count() > 0)
{
reqCandidateData = reqCandidateDt.CopyToDataTable();
}
}
catch (Exception ex)
{
}
return reqCandidateData;
}
public static System.Data.DataTable FilterCandidates(System.Data.DataTable FullCandidateData, System.Data.DataTable RequiredCandidateIDs)
{
System.Data.DataTable reqCandidateData = new System.Data.DataTable();
try
{
var reqCandidateDt = (from candidateRow in FullCandidateData.AsEnumerable()
join reqCandidateRow in RequiredCandidateIDs.AsEnumerable()
on
new { CandidateId = candidateRow["Candidate_ID"].ToString(), Job_Id = candidateRow["Job_ID"].ToString() }
equals
new { CandidateId = reqCandidateRow["Candidate_ID"].ToString(), Job_Id = reqCandidateRow["Job_Id"].ToString() }
select candidateRow);
if (reqCandidateDt.Count() > 0)
{
reqCandidateData = reqCandidateDt.CopyToDataTable();
}
}
catch (Exception ex)
{
}
return reqCandidateData;
}
public static System.Data.DataTable FilterCandidates(System.Data.DataTable FullCandidateData, System.Data.DataTable RequiredCandidateIDs)
{
System.Data.DataTable reqCandidateData = new System.Data.DataTable();
try
{
var reqCandIds = RequiredCandidateIDs.AsEnumerable().Select(reqCandidateRow => reqCandidateRow["Candidate_ID"].ToString());
var reqCandidateDt = (from candidateRow in FullCandidateData.AsEnumerable()
where !reqCandIds.Contains(candidateRow["Candidate_ID"].ToString())
select candidateRow);
if (reqCandidateDt.Count() > 0)
{
reqCandidateData = reqCandidateDt.CopyToDataTable();
}
}
catch (Exception ex)
{
}
return reqCandidateData;
}
答案 1 :(得分:0)
我的第一个数据表中有大约90,000个候选人,并且它一直在增长。
关于这一点,你可能需要开始设计/思考负载。
我建议: