C# 两种只在LINQ中不同的方法,其中part-delegate?
我有一个方法:C# 两种只在LINQ中不同的方法,其中part-delegate?,c#,linq,design-patterns,delegates,oop,C#,Linq,Design Patterns,Delegates,Oop,我有一个方法: internal List<int> GetOldDoctorsIDs { var Result = from DataRow doctor in DoctorTable.Rows where doctor.Age > 30 select doctor.ID List<int> Doctors = new List<int>(); foreach
internal List<int> GetOldDoctorsIDs
{
var Result = from DataRow doctor in DoctorTable.Rows
where doctor.Age > 30
select doctor.ID
List<int> Doctors = new List<int>();
foreach (int id in Result)
{
//Register getting data
Database.LogAccess("GetOldDoctorsID: " + id.ToString());
if (Database.AllowAccess(DoctorsTable, id))
{
Doctors.Add(id);
}
}
}
我将:
where doctor.Cost > 30000
如何为此创建优雅的面向对象解决方案?
我应该使用delegate还是其他东西?您可以有选择地添加where子句,例如:
var Result = from DataRow doctor in DoctorTable.Rows
select doctor.ID;
if(getByAge) {
Result=Result.Where(doctor => doctor.Age>30);
} else {
Result=Result.Where(doctor => doctor.Cost>30000);
}
其中,getByAge
将是方法上的一个布尔参数
编辑。如果需要对where子句进行参数化,应该可以使用类似的方法:
internal List<int> GetOldDoctorsIDs(Func<Doctor, bool> whereClause)
{
var Result = DoctorTable.Rows.Where(d => whereClause(d)).Select(d => d.ID);
//etc...
}
内部列表GetOldDoctorId(Func whereClause)
{
var Result=DoctorTable.Rows.Where(d=>Where子句(d)).Select(d=>d.ID);
//等等。。。
}
然后按照Heime所说的方式调用该方法。您可以参数化您的方法,使其将条件作为从DataRow到bool的函数 你打不来
GetDoctorsIDs(doctor => doctor.Age > 30);
或
如果修改方法以包含谓词参数(见下文),则可以根据Hemium的示例使用所需的任何过滤器调用该方法
internal List<int> GetDoctorsIDs(Predicate<DataRow> doctorFilter)
{
var Result = from DataRow doctor in DoctorTable.Rows
where doctorFilter(doctor)
select doctor.ID
List<int> Doctors = new List<int>();
foreach (int id in Result)
{
//Register getting data
Database.LogAccess("GetOldDoctorsID: " + id.ToString());
if (Database.AllowAccess(DoctorsTable, id))
{
Doctors.Add(id);
}
}
}
内部列表GetDoctorId(谓词doctorFilter)
{
var Result=来自DoctorTable.Rows中的DataRow医生
where博士过滤器(博士)
选择医生ID
列表医生=新列表();
foreach(结果中的int-id)
{
//获取数据的寄存器
LogAccess(“getOldDoctorId:+id.ToString());
if(Database.AllowAccess(DoctorsTable,id))
{
医生。添加(id);
}
}
}
我成功地做到了这一点:
internal List<int> GetOldDoctorsIDs()
{
return GetDoctorsIDs((doctor) => doctor.Age > 30);
}
internal List<int> GetExpensiveDoctorsIDs()
{
return GetDoctorsIDs((doctor) => doctor.Cost > 30000);
}
internal List<int> GetDoctorsIDs(Func<DataRow, bool> Condition)
{
var Result = from DataRow doctor in DoctorTable.Rows
where Condition(doctor)
select doctor.ID
List<int> Doctors = new List<int>();
foreach (int id in Result)
{
//Register getting data
Database.LogAccess("GetOldDoctorsID: " + id.ToString());
if (Database.AllowAccess(DoctorsTable, id))
{
Doctors.Add(id);
}
}
}
内部列表GetOldDoctorId()
{
返回GetDoctorsIDs((doctor)=>doctor.Age>30);
}
内部列表GetExpensivedOctorId()
{
返回GetDoctorsIDs((doctor)=>doctor.Cost>30000);
}
内部列表GetDoctorId(Func条件)
{
var Result=来自DoctorTable.Rows中的DataRow医生
何处情况(医生)
选择医生ID
列表医生=新列表();
foreach(结果中的int-id)
{
//获取数据的寄存器
LogAccess(“getOldDoctorId:+id.ToString());
if(Database.AllowAccess(DoctorsTable,id))
{
医生。添加(id);
}
}
}
是的,但如果在编写DoctorTable.Rows时,我将有6个仅在where部分更改的后续方法,该怎么办。我无法通过Intellisense Where获取数据,因此它不在那里,当我这样做时:在DoctorTable>Rows-Where(Rows=>Where子句)中选择DataRow-doctor“无法将lambda表达式转换为bool类型,因为它不是委托类型。您能更详细地描述此解决方案吗?这与我建议的Func差不多,其中TReturn的bool类型等同于谓词。
internal List<int> GetDoctorsIDs(Predicate<DataRow> doctorFilter)
{
var Result = from DataRow doctor in DoctorTable.Rows
where doctorFilter(doctor)
select doctor.ID
List<int> Doctors = new List<int>();
foreach (int id in Result)
{
//Register getting data
Database.LogAccess("GetOldDoctorsID: " + id.ToString());
if (Database.AllowAccess(DoctorsTable, id))
{
Doctors.Add(id);
}
}
}
internal List<int> GetOldDoctorsIDs()
{
return GetDoctorsIDs((doctor) => doctor.Age > 30);
}
internal List<int> GetExpensiveDoctorsIDs()
{
return GetDoctorsIDs((doctor) => doctor.Cost > 30000);
}
internal List<int> GetDoctorsIDs(Func<DataRow, bool> Condition)
{
var Result = from DataRow doctor in DoctorTable.Rows
where Condition(doctor)
select doctor.ID
List<int> Doctors = new List<int>();
foreach (int id in Result)
{
//Register getting data
Database.LogAccess("GetOldDoctorsID: " + id.ToString());
if (Database.AllowAccess(DoctorsTable, id))
{
Doctors.Add(id);
}
}
}