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

前置条件

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

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值