Guest User

Untitled

a guest
Oct 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net;
  7. using System.Data.SqlClient;
  8.  
  9.  
  10. namespace GlobalConnectStaff
  11. {
  12. class Program
  13. {
  14.  
  15. static void Main(string[] args)
  16. {
  17. gcstaffDataContext dc = new gcstaffDataContext();
  18. string strFileName = "globalconnectstaff.csv";
  19. string path = @"C:\GlobalConnectStaff";
  20. StreamWriter sw = File.CreateText(path + "\\" + strFileName);
  21.  
  22. var staff = (from a in dc.usps_employees
  23. join b in dc.usps_job_positions on a.EMPLOYEE_ID equals b.EMPLOYEE_ID
  24. select new
  25. {
  26. b.EMPLOYEE_ID,
  27. b.FULL_NAME,
  28. b.FIRST_NAME,
  29. b.LAST_NAME,
  30. b.SEX,
  31. a.PHONE,
  32. a.PHONE_IN_DISTRICT,
  33. b.BUILDING,
  34. a.EMAIL_ADDRESS
  35. }).Distinct();
  36.  
  37.  
  38. List<GcStaff> gcstaff = new List<GcStaff>();
  39.  
  40. foreach (var s in staff)
  41. {
  42. gcstaff.Add(new GcStaff
  43. {
  44. EmpId = Convert.ToInt32(s.EMPLOYEE_ID),
  45. FullName = s.FULL_NAME,
  46. First_Name = s.FIRST_NAME,
  47. Last_Name = s.LAST_NAME,
  48. Gender = s.SEX,
  49. Phone = s.PHONE,
  50. Phone2 = s.PHONE_IN_DISTRICT,
  51. Building = s.BUILDING,
  52. Email = s.EMAIL_ADDRESS
  53. });
  54.  
  55. }
  56.  
  57. string csvStaff = LinqToCSV.ToCsv<GcStaff>(gcstaff);
  58. sw.WriteLine("Employee_Reference_#,Full_Name,Fname,Lname,Gender,Phone #1, Phone #2, Location, Email_Address");
  59. sw.Write(csvStaff);
  60.  
  61. }
  62.  
  63.  
  64. }
  65. public static class LinqToCSV
  66. {
  67. public static string ToCsv<T>(this IEnumerable<T> items)
  68. where T : class
  69. {
  70. var csvBuilder = new StringBuilder();
  71. var properties = typeof(T).GetProperties();
  72. foreach (T item in items)
  73. {
  74. string line = string.Join(",", properties.Select(p => p.GetValue(item, null).ToCsvValue()).ToArray());
  75. csvBuilder.AppendLine(line);
  76. }
  77. return csvBuilder.ToString();
  78. }
  79.  
  80. private static string ToCsvValue<T>(this T item)
  81. {
  82. if (item == null) return "\"\"";
  83.  
  84. if (item is string)
  85. {
  86. return string.Format("\"{0}\"", item.ToString().Replace("\"", "\\\""));
  87. }
  88. double dummy;
  89. if (double.TryParse(item.ToString(), out dummy))
  90. {
  91. return string.Format("{0}", item);
  92. }
  93. return string.Format("\"{0}\"", item);
  94. }
  95. }
  96.  
  97. }
Add Comment
Please, Sign In to add comment