public static string GetCSVDirectory(string sort = "", string filter = "", directoryViewType viewType = directoryViewType.All, bool includeMobile = false) { List <DirectoryItem> dis = GetDirectory(1, 0, sort, filter, viewType, includeMobile).DirectoryItems; using (var ms = new System.IO.MemoryStream()) { StringBuilder sb = new StringBuilder(); sb.Append("Date:," + DateTime.Now.ToString("MM/dd/yyyy") + ","); sb.Append("Filter:,=\"" + ((filter.Length > 0) ? filter : "[None]") + "\","); sb.Append("Sort:," + ((sort.Length > 0) ? sort : "[Default]") + ","); sb.Append("Directory:," + viewType.ToString()); sb.Append("\n"); string value = sb.ToString(); ms.Write(Encoding.Default.GetBytes(value), 0, value.Length); // Get an empty DirectoryItem so we can get a CSV "Header" DirectoryItem dih = new DirectoryItem(); dih.SerializeCSVHeader(ms, includeMobile); foreach (DirectoryItem di in dis) { di.SerializeAsCsv(ms, includeMobile); } ms.Position = 0; var sr = new System.IO.StreamReader(ms); return(sr.ReadToEnd()); } }
/// <summary> /// Main function used for returning data. All parameters are optional. This /// function will use the other GetDirectory functions as needed to produce the /// final filter, sorted, paginated results. /// </summary> /// <param name="page">Which page of data to return, based on pageSize. Default is page 1.</param> /// <param name="pageSize">How many items are considered a page. Default is ALL items (1 page).</param> /// <param name="sort">Comma delimeted list of sort values that can contain ASC/DESC if needed: "RegionName DESC,Name". Default is LastNamePCName,FirstName. Field names must match DirectoryItem properties!</param> /// <param name="filter">Space delimited list of items to filter: "joe green 610".</param> /// <param name="viewType">directoryViewType to indicate a filter at the ViewType leverl. Default is "All".</param> /// <returns>List of filtered, sorted, paginated Directory Items in a Directory object which will also contain information about what data was returned.</returns> public static CompanyDirectory GetDirectory(int?page, int?pageSize, string sort = "", string filter = "", directoryViewType viewType = directoryViewType.All, bool includeMobile = false) { CompanyDirectory returnData = new CompanyDirectory(); List <DirectoryItem> returnItems; if (filter.Length > 0) { returnItems = GetDirectory(filter, includeMobile); } else { returnItems = GetDirectory(includeMobile); } switch (viewType) { case directoryViewType.All: break; case directoryViewType.ProfitCenter: returnItems.RemoveAll(x => x.IsProfitCenter == false); break; case directoryViewType.RegionManager: returnItems.RemoveAll(x => x.IsRegionManager == false); break; case directoryViewType.RegionSupport: returnItems.RemoveAll(x => x.IsRegionSupport == false); break; case directoryViewType.ServiceCenter: returnItems.RemoveAll(x => x.IsServiceCenter == false); break; case directoryViewType.ShowRoom: returnItems.RemoveAll(x => x.HasShowroom == false); break; default: break; } List <Tuple <string, string> > sortTuple = new List <Tuple <string, string> >(); if (sort == null || sort.Trim().Length == 0) { sort = "RegionNumberSort,PCSort,Name"; } foreach (string sortValue in sort.Split(',')) { if (sortValue.Contains(' ')) { sortTuple.Add(new Tuple <string, string>(sortValue.Split(' ')[0].Trim(), sortValue.Split(' ')[1].Trim())); } else { sortTuple.Add(new Tuple <string, string>(sortValue.Trim(), "asc")); } } if (page == null) { page = 1; } if ((pageSize == null) || (pageSize == 0)) { pageSize = returnItems.Count(); } int skip = (int)pageSize * ((int)page - 1); returnData.page = page ?? 1; // ?? Used to for clean conversion from nullable type even though we know it can't be null because of the code above. returnData.pageSize = pageSize ?? returnItems.Count(); // ?? Used to for clean conversion from nullable type even though we know it can't be null because of the code above. if (returnData.pageSize == 0) { returnData.pageSize = returnItems.Count(); } if (returnData.pageSize == 0) { returnData.pageSize = 1; // Incase count was 0 too! } returnData.totalPages = (int)Math.Ceiling(((decimal)returnItems.Count() / returnData.pageSize)); returnData.totalRecords = returnItems.Count(); returnData.showMobile = includeMobile; if (skip < returnItems.Count()) { returnData.DirectoryItems = returnItems.MultipleSort(sortTuple).Skip(skip).Take((int)pageSize).ToList(); } return(returnData); }