/// <summary> /// 汇总查询结果到缓存 /// </summary> public void JoinCatches(IEnumerable <T> models) { if (models != null && models.Count() > 0) { var modelUtil = MappingDataSet.GetMapper(typeof(T)).GetModelUtil(); if (modelUtil.PrimaryKeyPropertyName != null) { //如果是有主键标识的话,按主键合并 foreach (var m in models) { var res = ModelCatches.Where(a => modelUtil.GetPrimaryKeyValue(a) == modelUtil.GetPrimaryKeyValue(m)).ToList(); if (res.Count > 0) { //如果已经存在这个对象则合并 //遍历属性 foreach (var p in ElementType.GetProperties()) { if (modelUtil.GetPropertyValue(res[0], p.Name) == null) { modelUtil.SetPropertyValue(res[0], p.Name, modelUtil.GetPropertyValue(m, p.Name)); } } } else { //否则添加 ModelCatches.Add(m); } } } } }
public T Find(Func <T, bool> method) { //先从缓存中查找, 没有查到去数据库查找 var res = ModelCatches.Where(method).First(); if (res == null) { //查找数据库,并执行查询 this.Where(method).ToList(); return(ModelCatches.Where(method).First()); } else { return(res); } }