泛型与cache互转,并判断元素是否存在的方法
来源:互联网 发布:天气软件手机版 编辑:程序博客网 时间:2024/05/17 09:33
protected void Page_Load(object sender, EventArgs e) {
//存 List<Guid> sessionIDLists = new List<Guid>(); sessionIDLists.Add(new Guid("080F28AD-8A64-45A5-9CA2-03ECCAA12ADE")); sessionIDLists.Add(new Guid("ADAAD247-E7F9-4FB1-896D-0F7695009488")); sessionIDLists.Add(new Guid("357CFB6B-040E-45DF-BD8B-10B882142FBF")); sessionIDLists.Add(new Guid("01782AF2-29E2-49FD-8D68-256BCEB29940")); Cache["sessionIDLists"]=sessionIDLists; // Cache.Add("sessionIDLists", sessionIDLists, mydepen, DateTime.Now.AddSeconds(100), TimeSpan.Zero, CacheItemPriority.Normal, null); string sid = "357CFB6B-040E-45DF-BD8B-10B882142FBF"; if (Cache["sessionIDLists"] != null) {
//转化
List<Guid> sessionIDLists2 = Cache["sessionIDLists"] as List<Guid>; // if (MatchGuid(sessionIDLists2, new Guid(sid)))
if (sessionIDLists2.Contains(new Guid(sid))) { Response.Write("true"); } else { Response.Write("false"); } } // System.Collections.Generic.List<Guid> sessionIDLists = System.Web.Caching.Cache["sessionIDLists"]; }//判断是否存在
//private static bool MatchGuid(List<Guid> lists, Guid sid
//{ // bool returnValue = false; // foreach (Guid gid in lists) // { // if (gid == sid) // { // returnValue = true; // break; // } // else returnValue = false; // } // return returnValue;
//}
参考:
C# 中List类的Find、Exists、Sort, 等方法的应用举例
C# 中List类的Find,Exists,FindAll,FindIndex ,FindLast ,FindLastIndex等方法的应用举例
以Exists为例,MSDN中是这样叙述的
Exists确定 List<(Of<(T>)>)是否包含与指定谓词所定义的条件相匹配的元素。