A small application of ILookup


ILookup is a data structure, Lookup inherited from its ultimate parent is IEnumerable, can use all the methods of Linq,
iLookup is an upgraded version of the dictionary, the most suitable one to many of the most usage scenarios, you can use the index,
the following is a scenarios, the provincial corresponding number of prefecture-level city relationship

 

{ 
  ( Var itearea, _) = es.GetTCInfo (ItemID, type, stime, etime);
    IF (itearea.Count ()> 0 ) 
   { 
       the Type _type = itearea.ToList () [ 0 ] .GetType ();
        var the plist itearea.Select = (k => ( String ) _type.GetProperty ( " name " ) .getValue (k)); // names of all the provinces of up to 34 provincial-level administrative regions, up to 34 times traversed 
       var datasoure = plist.ToLookup (the p-=> the p-, pname => es.TpcitySubdivision (pname, itemid, of the type, stime, etime) .ToList ()); 
       AsposeCellUtil.ComposeExcel ($ " {(of the type == 1? " topic" : " Channel " )} DateTime.Now.ToString geographical details {( " yyyyMMddHHmmss " .)} XLS " , datasoure, new new  String [] { " city name " , " number " }); 
   } 
    the else 
      System.Web.HttpContext .Current.Response.Write ( " There is nothing to export! " ); 
}

 

       ///  <Summary> 
        /// composition [passed derived header table must match the number of source object data] field, does not support the basic type
         ///  </ Summary> 
        ///  <param name = "datasou"> data source </ param> 
        ///  <param name = "header"> header </ param> 
        public  static  void ComposeExcel <the source> ( String filename, ILookup < String , List <>> datasou the source, the params  String [] header ) WHERE the Source: class 
        { 
            ExectExportStream (filename, () =>
            {
                Workbook book = new Workbook(); 
                book.Worksheets.Clear();
                if (datasou.Count==0)
                    return null;
                
                foreach (var item in datasou)
                {
                    Worksheet worksheet = book.Worksheets.Add(item.Key);
                    Assemblesheet(ref worksheet, datasou[item.Key], header);
                }
                return book.SaveToStream();
            });
        }

        private static void Assemblesheet<Source>(ref Worksheet worksheet, IEnumerable<List<Source>> enumerable, params string[] header) 
        {
            if (enumerable.Count()==0)
                return;
            
            var list = enumerable.ToList()[0];  // 1 对 多 关系
            if (list.Count==0)
                return;

            Cells cells = worksheet.Cells;
            var pageCount = list.Count / importPageCount + (list.Count % importPageCount > 0 ? 1 : 0);
            PropertyInfo[] ps = list[0].GetType().GetProperties();
            bool isstr = list[0].GetType().IsValueType; //是否是值类型
            for (int j = 0; j < header.Length; j++)
                cells[0, j].PutValue(header[j]); //添加表头

            for (int i = 0; i < list.Count; i++)
            {
                for (int k = 0; k < header.Length; k++)
                {
               //    IF (ISSTR)
                //        cells [+ I. 1, K] .PutValue (List [I]); // insert data
                //     the else 
                       cells [ . 1 + I, K] .PutValue (PS [K] .getValue (List [I])); / / insert data
                     // header field with the number k = number of identical header table, ps attribute 
                } 
            } 
        }

 

 

Guess you like

Origin www.cnblogs.com/Qintai/p/11828261.html