最近寫案子常常用到List<T>,這個東西還真好用
因為它有下列東西:
List<T>.Sort() → 排序T
List<T>.Find() → 找出一個T
List<T>.FindAll() →找出多個T
List<T>.Exist() →判斷T是否存在
小弟就寫個範例介紹這些東西吧..
GenericList.aspx
01 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GenericList.aspx.cs" Inherits="GenericList" %> |
07 |
< title >GenericList</ title > |
10 |
< form id = "form1" runat = "server" > |
13 |
< asp:GridView ID = "GridView1" runat = "server" > |
GenericList.aspx.cs
002 |
using System.Collections.Generic; |
005 |
using System.Web.UI.WebControls; |
007 |
public partial class GenericList : System.Web.UI.Page |
010 |
protected void Page_Load( object sender, EventArgs e) |
012 |
List<Person> lstPerson = new List<Person>(); |
013 |
lstPerson.Add( new Person(1, "puma" , 10)); |
014 |
lstPerson.Add( new Person(2, "F6 Team" , 20)); |
015 |
lstPerson.Add( new Person(3, "ASP.NET" , 30)); |
016 |
lstPerson.Add( new Person(4, "Dotblogs" , 40)); |
019 |
this .GridView1.DataSource = lstPerson; |
020 |
this .GridView1.DataBind(); |
026 |
Response.Write( "找出Name='puma'的Person→ " ); |
027 |
Response.Write(lstPerson.Find( delegate (Person p) { return p.Name == "puma" ; }).ToString() + "<p>" ); |
033 |
Response.Write( "找出Age>10的數目→ " ); |
034 |
Response.Write(lstPerson.FindAll( delegate (Person p) { return p.Age > 10; }).Count.ToString() + "<p>" ); |
040 |
Response.Write( "檢查Name='F6'是否存在→ " ); |
041 |
Response.Write(lstPerson.Exists( delegate (Person p) { return p.Name == "F6" ; }).ToString() + "<p>" ); |
047 |
Response.Write( "<p>依Name升冪排序↑<br/>" ); |
048 |
lstPerson.Sort( delegate (Person p1, Person p2) { return Comparer< string >.Default.Compare(p1.Name, p2.Name); }); |
049 |
foreach (Person p in lstPerson) |
051 |
Response.Write(p.ToString() + "<br/>" ); |
058 |
Response.Write( "<p>依Name降冪排序↓<br/>" ); |
059 |
lstPerson.Sort( delegate (Person p1, Person p2) { return Comparer< string >.Default.Compare(p2.Name, p1.Name); }); |
060 |
foreach (Person p in lstPerson) |
062 |
Response.Write(p.ToString() + "<br/>" ); |
070 |
private string _Name; |
073 |
public Person( int ID, string Name, int Age) |
088 |
set { _Name = value; } |
089 |
get { return _Name; } |
094 |
set { _Age = value; } |
098 |
public override string ToString() |
100 |
return string .Format( "ID:{0},Name:{1},Age:{2}" , _ID, _Name, _Age); |
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SqlServerHelper;
using LogisticsMODEL;
using LogisticsDAL;
using Untity;
using System.Text;
namespace LogisticsBLL
{
public class B_Sys_Region : IB_Sys_Region
{
public static IList<M_Sys_Region> GetProvince()
{
return GetRegionList().FindAll(delegate(M_Sys_Region m) { return m.Parentid == 0; });
}
public static IList<M_Sys_Region> GetRegionList(int parentid)
{
return GetRegionList().FindAll(delegate(M_Sys_Region m) { return m.Parentid == parentid; });
}
/// <summary>
/// 根据编码查询地区名称
/// </summary>
/// <param name="_Codeid"></param>
/// <returns></returns>
public static M_Sys_Region GetRegionModes(int _Codeid)
{
Dictionary<int, M_Sys_Region> d_Region = CacheHelper.GetCache(B_CacheKey.Sys_Region_Dictionary) as Dictionary<int, M_Sys_Region>;
if (d_Region == null)
{
d_Region = new Dictionary<int, M_Sys_Region>();
IList<M_Sys_Region> _RegionList = new B_Sys_Region().SelectDataWhere(null);
foreach (M_Sys_Region sys_RegionModel in _RegionList)
{
d_Region.Add(sys_RegionModel.Codeid.Value, sys_RegionModel);
}
}
M_Sys_Region _M_Sys_Region = new M_Sys_Region();
return d_Region.TryGetValue(_Codeid, out _M_Sys_Region) ? _M_Sys_Region : null;
}
/// <summary>
/// 模糊搜索地区 返回地球对象和地区级别
/// </summary>
/// <param name="name">地区名称</param>
/// <param name="lv">1表示省 2城市 3表示区县</param>
/// <returns></returns>
public static M_Sys_Region GetRegionLikeModel(string name,out int lv) {
lv = -1;
const int Province = 2;//省的编号长度
const int City = 4;//市的编号长度
const int County = 6;//县区的编号长度
M_Sys_Region _M_Sys_Region=GetRegionList().Find(delegate (M_Sys_Region m){ return m.Name.IndexOf(name)>-1; });
if (_M_Sys_Region != null && _M_Sys_Region.Codeid != null)
{
switch (_M_Sys_Region.Codeid.Value.ToString().Length)
{
case Province: lv = 1; break;
case City: lv = 2; break;
case County: lv = 3; break;
}
}
return _M_Sys_Region;
}
public static List<M_Sys_Region> GetRegionList()
{
List<M_Sys_Region> syslist = CacheHelper.GetCache(B_CacheKey.Sys_Region) as List<M_Sys_Region>;
if (syslist == null)
{
syslist = new B_Sys_Region().SelectDataWhere(null) as List<M_Sys_Region>;
CacheHelper.SetCache(B_CacheKey.Sys_Region, syslist);
}
return syslist;
}
}
}