linq查询条件参数化--解决实际问题记录
2018-02-11 17:43
651 查看
前置条件
单位批量加解密方法脱离世纪,太难用了。返回字典。并且限制每次批量100.
实际使用中一般都是实体类集合中的某一个或某几个字段需要解密。
基于以上原因原有的无法满足需求,重新封装。
使用linq+反射完成。其中linq查询条件参数化实际上就是委托。
代码如下:
/// <summary> /// 转换实体集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list">需要转换的集合</param> /// <param name="selector">需要解密的字段</param> /// <param name="propertyGet">解密字段</param> /// <param name="propertySet">解密后的值需赋值的字段</param> /// <returns>返回解密后的实体集合</returns> public static List<T> DecryptByModels<T>(IEnumerable<T> list, Func<T, string> selector, string propertyGet, string propertySet) where T : new() { var myList = list.ToList(); var dictionary = SecurityHelper.Decrypt<T>(myList, selector); int len = myList.Count(); for (int i = 0; i < len; i++) { myList[i] = SecurityHelper.SetPropertyValue<T>(myList[i], propertyGet, propertySet, dictionary); } return myList; } /// <summary> /// 实体集合传入需要解密字段,并返回批量解密数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="selector"></param> /// <returns></returns> private static Dictionary<string, string> Decrypt<T>(this IEnumerable<T> source, Func<T, string> selector) where T : new() { var result = source.Select(selector).Where(p => !string.IsNullOrWhiteSpace(p)).Distinct().ToList(); int cutNum = (result.Count / 100) + ((result.Count % 100) > 0 ? 1 : 0); Dictionary<string, string> dictionary = new Dictionary<string, string>(); for (int i = 0; i < cutNum; i++) { var dictionaryBatch = SecurityHelper.DecryptBatch(result.Skip(i * 100).Take(100).ToList()); dictionary = dictionary.Concat(dictionaryBatch).ToDictionary(p => p.Key, p => p.Value); } return dictionary; } /// <summary> /// 集合解密后数据赋值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <param name="getProperty"></param> /// <param name="setProperty"></param> /// <param name="dictionary"></param> /// <returns></returns> private static T SetPropertyValue<T>(T t, string getProperty, string setProperty, Dictionary<string, string> dictionary) { PropertyInfo getPropertyInfo = t.GetType().GetProperty(getProperty); PropertyInfo setPropertyInfo = t.GetType().GetProperty(setProperty); var getValue = getPropertyInfo.GetValue(t, null); //为空不赋值了 if (getValue == null || string.IsNullOrWhiteSpace(getValue.ToString())) { return t; } var result = dictionary.Where(p => p.Key == getPropertyInfo.GetValue(t, null).ToString()).Select(p => p.Value).ToList(); string value = result.Count > 0 ? result.First() : ""; setPropertyInfo.SetValue(t, value); return t; }
调用方式
List<Test> list = new List<Test>(); list.Add(new Test() { a1 = "E8637AE9A790EA36A8E2353B178BED159099DD8FBD5EA5EBBFDEAF70809425F2" }); list.Add(new Test() { a1 = "71A094E8F88DBF5F21CCA394DBE094E4971050E8A935BEB2AC26875FA0F541D8" }); list = SecurityHelper.DecryptByModels(list, p => p.a1, "a1", "a2");
Test类
public class Test { public string a1 { get; set; } public string a2 { get; set; } }
相关文章推荐
- linq to sql 中,如何解决多条件查询问题,答案,用表达式树! (下)
- laravel 解决leftjoin带条件查询没有返回右表为NULL的记录问题
- linq to sql 中,如何解决多条件查询问题,答案,用表达式树!
- linq to sql 中,如何解决多条件查询问题,答案,用表达式树! (下)
- linq to sql 中,如何解决多条件查询问题,答案,用表达式树! (下)
- linq to sql 中,如何解决多条件查询问题,答案,用表达式树! (下)
- linq to sql中,如何解决多条件查询问题,答案,用表达式树!
- Linq实体类的设计(解决了复合查询的问题,同时解决了LINQ上下文缓存问题)
- Mybatis 查询int类型数据,返回记录条数为0时报错问题解决
- Linq to Sql 或linq to entities 与SQL 查询结果不一致,返回重复结果问题解决方法
- 小小问题集锦6之---查询条件非常复杂时解决效率不高的一个办法
- 解决LINQ多个DataContext查询问题
- SqlServer使用 case when 解决多条件模糊查询问题
- 【记录】正则表达式学习第3天(正则学习笔记),又解决了个实际问题。
- YII带查询条件的分页问题解决
- 解决pl/sql developer 中文字段显示乱码或无法用中文作查询条件的问题
- mysql中RAND()随便查询记录效率问题和解决办法分享
- Linq 多条件查询组合问题
- 巧用case when 解决多条件模糊查询问题