linq查询条件参数化--解决实际问题记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gzy11/article/details/79312328

前置条件

单位批量加解密方法脱离世纪,太难用了。返回字典。并且限制每次批量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; }

    }

猜你喜欢

转载自blog.csdn.net/gzy11/article/details/79312328