Advertisement
Guest User

LinQ SO Question

a guest
Jul 21st, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. public partial class CircuitInstallationScheduledWindow : Window
  2.     {
  3.         DataSet mainWorkbook, Access;
  4.         string tSubject, tCC, tHTML;
  5.         DataTable previewTable;
  6.         public CircuitInstallationScheduledWindow(DataSet mainWorkbook, string tSubject, string tCC, string tHTML)
  7.         {
  8.             this.tSubject = tSubject;
  9.             this.tCC = tCC;
  10.             this.tHTML = tHTML;
  11.             this.mainWorkbook = mainWorkbook;
  12.             InitializeComponent();
  13.             Access = ABFS_Database.getTables();
  14.             previewTable = MakePreview();            
  15.             previewGrid.ItemsSource = previewTable.AsDataView();
  16.            
  17.  
  18.            
  19.         }
  20.         public DataTable MakePreview()
  21.         {
  22.             //gets all rows from the "Salesforce Report Weekly" sheet with FOC dates which are today or in the future.
  23.             var table = (from row in mainWorkbook.Tables["Salesforce Report Weekly"].AsEnumerable()
  24.                          where row.Field<DateTime?>("Target Circuit Completion (FOC)") >= DateTime.Today
  25.                          select row).AsDataView().ToTable(false, "Opportunity: Store Number", "Target Circuit Completion (FOC)", "Vendor Name");
  26.  
  27.             //gets all rows from the ALreadySent table where the Notification Type is "Install Scheduled".
  28.             var sentCIS = (from row in Access.Tables["AlreadySent"].AsEnumerable()
  29.                            where row.Field<string>("NotificationType") == "Install Scheduled"
  30.                            select row).AsDataView().ToTable();
  31.             //filters out rows in 'table' which were already sent and recorded in the Access database.
  32.             var filteredTable = table.AsEnumerable()
  33.                 .Where(t1 => !sentCIS.AsEnumerable()
  34.                     .Any(t2 => t2.Field<int>("StoreNumber") == t1.Field<double>("Opportunity: Store Number")
  35.                             && Math.Abs((t2.Field<DateTime>("TargetCircuitCompletionFOC") - t1.Field<DateTime>("Target Circuit Completion (FOC)")).TotalDays)<1)).AsDataView().ToTable();
  36.             //join filtered table with McDonalds Contact Master.
  37.             var contacts = mainWorkbook.Tables["MCD Contact Master"].AsDataView().ToTable(false, "National Store .","Contacts - ACM - Email", "OO - Ops Mgr Name",
  38.                                                                                     "OO - Ops Mgr Name", "Contacts - Area Sup / BC - Email", "Contacts - OTP - Email", "OTM Email Address");
  39.             DataTable resultTable = new DataTable();
  40.             foreach(string name in new string[]{"Opportunity: Store Number", "Target Circuit Completion (FOC)", "Vendor Name","Contacts - ACM - Email", "OO - Ops Mgr Name", "OO - Ops Mgr Name", "Contacts - Area Sup / BC - Email", "Contacts - OTP - Email", "OTM Email Address"})
  41.             {
  42.                 resultTable.Columns.Add(name);
  43.             }
  44.  
  45.             var joinedTables =  from tableRow in filteredTable.AsEnumerable()
  46.                                 join contactsRow in contacts.AsEnumerable()
  47.                                 on tableRow.Field<double>("Opportunity: Store Number") equals contactsRow.Field<double>("National Store .")
  48.                                 into lj
  49.                                 from r in lj.DefaultIfEmpty()
  50.                                 select resultTable.LoadDataRow(new object[]
  51.                                 {
  52.                                     tableRow.Field<double>("Opportunity: Store Number"),
  53.                                     tableRow.Field<DateTime>("Target Circuit Completion (FOC)"),
  54.                                     tableRow.Field<string>("Vendor Name"),
  55.                                     contactsRow.Field<string>("Contacts - ACM - Email")
  56.                                 }, false);
  57.  
  58.            
  59.            
  60.            
  61.         }
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement