﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ICaiBan.Framework.ORM
{
    public static class Extensions
    {
        #region 扩展 List<T> 

        /// <summary>
        ///  转换为自定义数据模型列表
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static EntityList<TSource> ToEntityList<TSource>(this List<TSource> source) where TSource : Entity<TSource>, new()
        {
            EntityList<TSource> reList=new EntityList<TSource>();
            reList.AddRange(source);
            return reList;
        }

        /// <summary>
        /// 转换为json 数据字符串
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <param name="returnFields"></param>
        /// <returns></returns>
        public static string ToJson<TSource>(this List<TSource> source, FieldInfo[] returnFields) where TSource : Entity<TSource>, new()
        {
            System.Text.StringBuilder sbJson=new System.Text.StringBuilder();

            foreach(var item in source)
            {
                if(sbJson.Length > 0)
                    sbJson.Append(",");
                sbJson.Append(item.ToJson(returnFields));
            }

            return "[" + sbJson.ToString() + "]";
        }

        #endregion
    }
}