ServiceCommon/EntityMapper/ListExtensions.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Domain;
using ServiceCommon.EntityMapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using ViewModel;

namespace ServiceCommon
{
    public static class ListExtensions
    {
        /// <summary>
        /// 映射实体
        /// </summary>
        /// <typeparam name="T">源实体</typeparam>
        /// <typeparam name="T1">目标实体</typeparam>
        /// <param name="list"></param>
        /// <param name="mappRule">映射规则(如:w=>new Role{ Id = w.Id , Name = w.DescName }) ;同名可忽略不写</param>
        /// <returns></returns>
        public static IList<Ttarget> MapTo<Tsource, Ttarget>(this IEnumerable<Tsource> list, Expression<Func<Tsource, Ttarget>> mappRule = null)
            where Tsource : new()
            where Ttarget : new()
        {
            IList<Ttarget> listTarget = new List<Ttarget>();

            if (mappRule == null) 
            {
                mappRule = w => new Ttarget { };
            }

            if (list == null || !list.Any()) 
            {
                return listTarget;
                //listTarget.Add(new Ttarget());
                //return listTarget;
            }

            MapperExpressionVisitor<Tsource, Ttarget> mv = new MapperExpressionVisitor<Tsource, Ttarget>();
            var exp =  mv.Convert(mappRule);

            
            foreach (var item in list)
            {
                listTarget.Add(exp.Invoke(item));
            }

            return listTarget;
        }

        /// <summary>
        /// ViewModel映射成Domain
        /// </summary>
        /// <typeparam name="Tsource">ViewModel</typeparam>
        /// <typeparam name="Ttarget">Domain</typeparam>
        /// <param name="model"></param>
        /// <param name="mappRule">映射规则(如:w=>new Role{ Id = w.Id , Name = w.DescName }) ;同名可忽略不写</param>
        /// <returns></returns>
后面还有 49 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。