我们主要是使用List<T>.FindAll(Predicate<T> match)进行筛选
其中Predicate<T>类型的参数,封装了筛选的规则。Predicate<T>是一个泛型委托,这意味着match参数是一个返回bool类型的方法,在FindAll()内部,会调用我们传递进去的这个方法。
public delegate bool Predicate<T>(T obj);
在讲述之前我们需要先复习一下几个概念
首先理解一下什么叫多态。同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果,这就是多态性。
多态性通过派生类覆写基类中的虚函数型方法来实现
来个例子:
abstract
class
MyAbs
{
public
abstract
void
AbMethod1();
public
abstract
void
AbMethod2();
}
class
MyClass1 : MyAbs
{
public
override
void
AbMethod1()
{
Console.WriteLine(
"
Abstarct method #1 of MyClass1
"
);
}
public
override
void
AbMethod2()
{
Console.WriteLine(
"
Abstarct method #2 of MyClass1
"
);
}
}
class
MyClient
{
public
static
void
Main()
{
MyAbs ma1
=
new
MyClass1();
//
这里就是多态
ma1.AbMethod1();
ma1.AbMethod2();
}
}
然后我们看下 如何来实现基于业务对象的筛选,主要来自 jimmyzhang的文章
页面上的代码
<
asp:ObjectDataSource ID
=
"
objdsOrderList
"
runat
=
"
server
"
SelectMethod
=
"
GetList
"
TypeName
=
"
OrderManager
"
OnSelecting
=
"
objdsOrderList_Selecting
"
>
<
SelectParameters
>
<
asp:Parameter Name
=
"
fullList
"
Type
=
"
Object
"
/>
<
asp:Parameter Name
=
"
filter
"
Type
=
"
Object
"
/>
</
SelectParameters
>
</
asp:ObjectDataSource
>
<
asp:DropDownList ID
=
"
ddlYear
"
runat
=
"
server
"
OnSelectedIndexChanged
=
"
ddlYear_SelectedIndexChanged
"
AutoPostBack
=
"
True
"
>
<
asp:ListItem Value
=
"
0
"
>
全部
</
asp:ListItem
>
<
asp:ListItem
>
2011
</
asp:ListItem
>
</
asp:DropDownList
>
<
asp:DropDownList ID
=
"
ddlMonth
"
runat
=
"
server
"
OnSelectedIndexChanged
=
"
ddlMonth_SelectedIndexChanged
"
AutoPostBack
=
"
True
"
>
<
asp:ListItem Value
=
"
0
"
>
全部
</
asp:ListItem
>
</
asp:DropDownList
>
<
asp:DropDownList ID
=
"
ddlDay
"
runat
=
"
server
"
OnSelectedIndexChanged
=
"
ddlDay_SelectedIndexChanged
"
AutoPostBack
=
"
True
"
>
<
asp:ListItem Value
=
"
0
"
>
全部
</
asp:ListItem
>
</
asp:DropDownList
>
<
asp:DropDownList ID
=
"
ddlCountry
"
runat
=
"
server
"
AutoPostBack
=
"
True
"
>
<
asp:ListItem Value
=
"
0
"
>
国家
</
asp:ListItem
>
<
asp:ListItem Value
=
"
中国
"
>
中国
</
asp:ListItem
>
<
asp:ListItem Value
=
"
法国
"
>
法国
</
asp:ListItem
>
</
asp:DropDownList
><
br
/>
<
asp:GridView ID
=
"
gvOrderList
"
runat
=
"
server
"
DataSourceID
=
"
objdsOrderList
"
>
<
Columns
>
<
asp:BoundField DataField
=
"
orderid
"
HeaderText
=
"
订单ID
"
/>
<
asp:BoundField DataField
=
"
customerid
"
HeaderText
=
"
客户ID
"
/>
<
asp:BoundField DataField
=
"
orderdate
"
HeaderText
=
"
订单日期
"
/>
<
asp:BoundField DataField
=
"
country
"
HeaderText
=
"
国家
"
/>
</
Columns
>
</
asp:GridView
>
主要看下 objectdatasouce 里面的typename表示使用的类,parameter是select方法的两个参数,首先会调用这个方法来绑定。
看下后台代码:
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack)
{
AppendListItem(ddlMonth,
12
);
AppendListItem(ddlDay,
31
);
}
}
public
int
Year
{
get
{
return
Convert.ToInt32(ddlYear.SelectedValue); }
}
public
int
Month
{
get
{
return
Convert.ToInt32(ddlMonth.SelectedValue); }
}
public
int
Day
{
get
{
return
Convert.ToInt32(ddlDay.SelectedValue); }
}
public
string
Country
{
get
{
return
ddlCountry.SelectedItem.Text.ToString(); }
}
//
获取查询语句
public
string
QuerySql
{
get
{
int
year
=
Year;
int
month
=
Month;
int
day
=
Day;
List
<
string
>
sqlList
=
new
List
<
string
>
();
string
subSql
=
string
.Empty;
if
(year
!=
0
)
{
sqlList.Add(
string
.Format(
"
Year(orderdate)={0}
"
, year));
}
if
(month
!=
0
)
{
sqlList.Add(
string
.Format(
"
Month(orderdate)={0}
"
, month));
}
if
(day
!=
0
)
{
sqlList.Add(
string
.Format(
"
Day(orderdate)={0}
"
, day));
}
if
(sqlList.Count
>
0
)
//
如果选择了任意一个下拉框,那么就拼装SQL语句
{
string
[] list
=
sqlList.ToArray();
subSql
=
"
where (
"
+
string
.Join(
"
and
"
, list)
+
"
)
"
;
}
return
"
select customerid,country,orderdate,orderid from Orders
"
+
subSql;
}
}
//
为列表添加项目
protected
void
AppendListItem(ListControl list,
int
end)
{
for
(
int
i
=
1
; i
<=
end; i
++
)
{
list.Items.Add(
new
ListItem(i.ToString()));
}
}
protected
void
objdsOrderList_Selecting(
object
sender, ObjectDataSourceSelectingEventArgs e)
{
//
e.InputParameters["query"] = QuerySql;
e.InputParameters[
"
fulllist
"
]
=
OrderManager.GetList();
//
获取全部列表
e.InputParameters[
"
filter
"
]
=
Filter;
}
protected
void
ddlYear_SelectedIndexChanged(
object
sender, EventArgs e)
{
gvOrderList.DataBind();
}
protected
void
ddlMonth_SelectedIndexChanged(
object
sender, EventArgs e)
{
gvOrderList.DataBind();
}
protected
void
ddlDay_SelectedIndexChanged(
object
sender, EventArgs e)
{
gvOrderList.DataBind();
}
public
DateFilter
<
Order
>
Filter
{
get
{
DateFilter
<
Order
>
filter
=
new
OrderFilter(Year, Month, Day,Country);
return
filter;
}
}
还有用于筛选的类 后面我会放上全部代码 其中sqlhelper类 大家直接用微软的就OK 我就不贴了 直接放上 几个类的代码/Files/danyingjie/App_Code.rar
希望对您有帮助。