我有一个类有两个非常相似的方法:
public class MyClass
{
public MyClass ()
{
}
public IQueryable<tbl_Person> PersonSelect1()
{
var ctx = new MyContextSQL();
var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
(x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
&& (x.department != 90 && x.department != 94) // only difference
&& (x.workID == 789 || x.workID == 567)
&& (x.clientID != 789247)
&& (x.auditID != 9)
&& (x.expDate >= new DateTime(2017, 1, 1))
&& (x.expDate <= DateTime.Today));
var result = temp.OrderByDescending(x => x.expDate);
return result;
}
public IQueryable<tbl_Person> PersonSelect2()
{
var ctx = new MyContextSQL();
var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
(x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
&& (x.department == 90 || x.department == 94) //only difference
&& (x.workID == 789)
&& (x.clientID != 789247)
&& (x.auditID != 9)
&& (x.expDate >= new DateTime(2017, 1, 1))
&& (x.expDate <= DateTime.Today));
var result = temp.OrderByDescending(x => x.expDate);
return result;
}
}
PersonSelect1()
和PersonSelect2()
之间只有一行不同我不喜欢它的样子,因为有很多代码重复。
我一直在考虑像这样解决它:
public IQueryable<tbl_Person> ThirdMethod()
{
var ctx = new MyContextSQL();
var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
(x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
&& (x.workID == 789)
&& (x.clientID != 789247)
&& (x.auditID != 9)
&& (x.expDate >= new DateTime(2017, 1, 1))
&& (x.expDate <= DateTime.Today));
var result = temp.OrderByDescending(x => x.expDate);
return result;
}
public IQueryable<tbl_Person> PersonSelect1()
{
var result = this.ThirdMethod().Where(x => (x.department != 90 || x.department != 94));
return result;
}
public IQueryable<tbl_Person> PersonSelect2()
{
var result = this.ThirdMethod().Where(x => (x.department == 90 || x.department == 94));
return result;
}
考虑到ThirdMethod()
将连接到数据库,然后从PersonSelect1()
或PersonSelect2()
,我会再次针对ThirdMethod()
调用产生的集合进行循环,这是最好的方法吗?我正在考虑性能并试图同时避免重复的代码。
答案 0 :(得分:1)
您可以创建一个共享逻辑的SQL视图,让C#只执行不同的过滤器,而不是尝试让应用程序共享逻辑。
这也会提高性能,因为视图执行速度比LINQ2SQL快。
答案 1 :(得分:1)
下面的变量名称很糟糕,语法可能会关闭,(我已经离开.net几年了)但是最简单的事情就是ThirdMethod
除了一些参数并构建相应地查询:
public IQueryable<tbl_Person> ThirdMethod(bool isFirstPerson)
{
var ctx = new MyContextSQL();
var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
(x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
&& isfirstPerson ?
(x.department != 90 && x.department != 94) : (x.department == 90 || x.department == 94)
&& (x.workID == 789)
&& (x.clientID != 789247)
&& (x.auditID != 9)
&& (x.expDate >= new DateTime(2017, 1, 1))
&& (x.expDate <= DateTime.Today));
var result = temp.OrderByDescending(x => x.expDate);
return result;
}
public IQueryable<tbl_Person> PersonSelect1()
{
return this.ThirdMethod(true);
}
public IQueryable<tbl_Person> PersonSelect2()
{
return this.ThirdMethod(false);
}
答案 2 :(得分:0)
不确定,但你可以这样做吗?
public IQueryable<tbl_Person> PersonSelect()
{
var ctx = new MyContextSQL();
var temp = ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x =>
(x.int_statID == 2 || x.int_statID == 1 || x.int_statID == 9)
&& (x.workID == 789 || x.workID == 567)
&& (x.clientID != 789247)
&& (x.auditID != 9)
&& (x.expDate >= new DateTime(2017, 1, 1))
&& (x.expDate <= DateTime.Today));
var result = temp.OrderByDescending(x => x.expDate);
return result;
}
public IQueryable<tbl_Person> PersonSelect1()
{
var ctx = PersonSelect();
ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x => (
(x.department != 90 && x.department != 94);
return result;
}
public IQueryable<tbl_Person> PersonSelect2()
{
var ctx = PersonSelect();
ctx.tbl_Person.Include("tbl_Student").Include("Department").Where(x => (
(x.department == 90 && x.department == 94);
return result;
}