Thursday, 21 May 2020

C# - LINQ (Join Table On Multiple Columns, Inside Method Call)


private static int count = 0;

private static int GetUnknownCount(IEnumerable<string> Bibs, 
                                   IEnumerable<string> LotNum, 
                                   IEnumerable<string> SplitLevel, 
                                   IEnumerable<string> ReburnLevel)
 {
     List<LotReport> lotreport2 = lotreport.Where(a => a.Process == "BurnIn").ToList();
     count = 0;

     List<string> mbib = Bibs.ToList();
     List<string> mLotNum = LotNum.ToList();
     List<string> mSplitLevel = SplitLevel.ToList();
     List<string> mReburnLevel = ReburnLevel.ToList();

     for (int i = 0; i < mbib.Count(); i++)
     {
         var bib = mbib[i].ToString();
         var lot = mLotNum[i].ToString();
         var splitlevel = mSplitLevel[i].ToString();
         var reburnlevel = mReburnLevel[i].ToString();

         foreach (var item in lotreport2)
         {
             if (item.LotNum == lot && item.SplitLevel == splitlevel
                  && item.ReburnLevel == reburnlevel && item.BIB == bib)
             {
                 if (item.ResultType == "Unknown")
                 {
                     count += int.Parse(item.Total);
                 }
             }

         }
     }
     return count;
 } 


private void button1_Click(object sender, EventArgs e)
{           

    List<LotReport> lotreport1 = lotreport.Where(a => a.Process == "Bin2").ToList();

    List<LotReport> lotreport2 = lotreport.Where(a => a.Process == "BurnIn").ToList();


    // Join Table On Multiple Columns
    var list3 =  from t1 in lotreport1
                join t2 in lotreport2
                on new { X = t1.LotNum, Y = t1.SplitLevel, Z = t1.ReburnLevel, 
                         W = t1.BIB ,  V = t1.ResultType } equals
                   new { X = t2.LotNum, Y = t2.SplitLevel, Z = t2.ReburnLevel,
                         W = t2.BIB, V = t2.ResultType }
                select new Table
                {
                    LotNum = t1.LotNum,
                    Process = t1.Process,
                    FreshLot = t1.FreshLot,
                    SplitLevel = t1.SplitLevel,
                    ReburnLevel = t1.ReburnLevel,
                    BIB = t1.BIB,
                    FD_Bin2 = t1.FD_Bin2,
                    FD_BurnIn = t2.FD_BurnIn,
                    ResultType = t1.ResultType,
                    Total = t1.Total,
                    ZeroCount = t1.ZeroCount.ToString(),
                    UnknownCount = t1.UnknownCount.ToString()
                };


    // Inside Method Call
    IEnumerable<TableB> list4 = lotreport1.GroupBy(a => new {
                                      a.FreshLot,

                                    })
                                    .Select(b => new TableB {

                                        LotNum = b.Select(c => c.LotNum),
                                        Process = b.Select(c => c.Process),
                                        FreshLot = b.Select(c => c.FreshLot),
                                        SplitLevel = b.Select(c => c.SplitLevel),
                                        ReburnLevel = b.Select(c => c.ReburnLevel),
                                        BIB = b.Select(c => c.BIB),
                                        FD_Bin2 = b.Select(c => c.FD_Bin2),
                                        FD_BurnIn = b.Select(c => c.FD_BurnIn),
                                        ResultType = b.Select(c => c.ResultType),
                                        Total = b.Select(c => c.Total),

                                        ZeroCount = b.Sum(c => c.ResultType == "Pass" ?
                                                               (c.Total == "0" ? 1 : 0 ) : 0 ),

                                        UnknownCount = GetUnknownCount(b.Select(c =>
                                                                  c.ResultType == "Pass" ?
                                                                  (c.Total == "0" ?
                                                                  c.BIB : string.Empty) :
                                                                  string.Empty),
                                                                 b.Select(c =>
                                                                  c.ResultType == "Pass" ?
                                                                  (c.Total == "0" ?
                                                                  c.LotNum : string.Empty) :
                                                                  string.Empty),
                                                                 b.Select(c =>
                                                                  c.ResultType == "Pass" ?  
                                                                  (c.Total == "0" ?
                                                                  c.SplitLevel : string.Empty) :
                                                                  string.Empty),
                                                                 b.Select(c =>
                                                                  c.ResultType == "Pass" ?
                                                                  (c.Total == "0" ?
                                                                  c.ReburnLevel :
                                                                  string.Empty) :
                                                                         string.Empty)),

                                     }).ToList();
           

}






Tuesday, 19 May 2020

C# - LINQ (Not Finished Yet)

Sort Algorithms:
Quick
Heap
Merge
Bubble
Insert
Shell
Select

Array Extensions
Rotate
Flip
Resize
Split
Slice
Fuse
Replace
Enumerate any array
Convert enumerable to multi-dimensional arrays

Pivot Sub Collections
Sequence Generation
Sequence Manipulation
Arithmetic Operations
Statistical Analysis
Sequence Analysis
Pivot Operations
Align and Match

C# - Two Ways To Measure Time In C#


Two Ways To Measure Time In C# .NET

//DateTime and TimeSpan
DateTime start = DateTime.UtcNow;
Thread.Sleep(2000);
DateTime end = DateTime.UtcNow;
TimeSpan timeDiff = end - start;
Console.WriteLine(Convert.ToInt32(timeDiff.TotalMilliseconds));

//Stopwatch
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Thread.Sleep(2000);
stopwatch.Stop();
TimeSpan stopwatchElapsed = stopwatch.Elapsed;
Console.WriteLine(Convert.ToInt32(stopwatchElapsed.TotalMilliseconds));


Convert Milliseconds To Human Readable Time Lapse

TimeSpan t = TimeSpan.FromMilliseconds(ms);
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",
                        t.Hours,
                        t.Minutes,
                        t.Seconds,
                        t.Milliseconds);



Tuesday, 12 May 2020

C++ - Dense and Sparse Multi-Dimensional Arrays

A sparse array is an array of data in which many elements have a value of zero. This is in contrast to a dense array, where most of the elements have non-zero values or are “full” of numbers. A sparse array may be treated differently than a dense array in digital data handling.

Tuesday, 28 April 2020

C# - Nullable type or ? on variable


Nullable<T> type or  ? on variable

Represents a value type that can be assigned null.
It's a nullable value. Structs, by default, cannot be nullable, they must have a value, so in C# 2.0, the Nullable<T> type was introduced to the .NET Framework.
C# implements the Nullable<T> type with a piece of syntactic sugar, which places a question mark after the type name, thus making the previously non-nullable type, nullable.

Explanation 

   //Cannot Be Null
   DateTime
   DateTime dt = null;   // Error: Cannot convert null to 'System.DateTime'
                         //because it is a  non-nullable value type
   //Can Be Null
   DateTime?             //Nullable < DateTime >               
   DateTime? dt = null;  // no problems



Example 1
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System;

class Sample
{
    // Define the "titleAuthor" table of the Microsoft "pubs" database.
    public struct titleAuthor
    {
        // Author ID; format ###-##-####
        public string au_id;
        // Title ID; format AA####
        public string title_id;
        // Author ORD is nullable.
        public short? au_ord;
        // Royalty Percent is nullable.
        public int? royaltyper;
    }

    public static void Main()
    {
        // Declare and initialize the titleAuthor array.
        titleAuthor[] ta = new titleAuthor[3];
        ta[0].au_id = "712-32-1176";
        ta[0].title_id = "PS3333";
        ta[0].au_ord = 1;
        ta[0].royaltyper = 100;

        ta[1].au_id = "213-46-8915";
        ta[1].title_id = "BU1032";
        ta[1].au_ord = null;
        ta[1].royaltyper = null;

        ta[2].au_id = "672-71-3249";
        ta[2].title_id = "TC7777";
        ta[2].au_ord = null;
        ta[2].royaltyper = 40;

        // Display the values of the titleAuthor array elements, and
        // display a legend.
        Display("Title Authors Table", ta);
        Console.WriteLine("Legend:");
        Console.WriteLine("An Author ORD of -1 means no value is defined.");
        Console.WriteLine("A Royalty % of 0 means no value is defined.");
    }

    // Display the values of the titleAuthor array elements.
    public static void Display(string dspTitle,
                               titleAuthor[] dspAllTitleAuthors)
    {
        Console.WriteLine("*** {0} ***", dspTitle);
        foreach (titleAuthor dspTA in dspAllTitleAuthors)
        {
            Console.WriteLine("Author ID ... {0}", dspTA.au_id);
            Console.WriteLine("Title ID .... {0}", dspTA.title_id);
            Console.WriteLine("Author ORD .. {0}", dspTA.au_ord ?? -1);
            Console.WriteLine("Royalty % ... {0}", dspTA.royaltyper ?? 0);
            Console.WriteLine();
        }
    }
}
// The example displays the following output:
//     *** Title Authors Table ***
//     Author ID ... 712-32-1176
//     Title ID .... PS3333
//     Author ORD .. 1
//     Royalty % ... 100
//    
//     Author ID ... 213-46-8915
//     Title ID .... BU1032
//     Author ORD .. -1
//     Royalty % ... 0
//    
//     Author ID ... 672-71-3249
//     Title ID .... TC7777
//     Author ORD .. -1
//     Royalty % ... 40
//    
//     Legend:
//     An Author ORD of -1 means no value is defined.
//     A Royalty % of 0 means no value is defined.