Wednesday, 6 June 2018

Create Dynamic Object on the fly in C#

Many times we required Object on the fly, at run time. On that case
System.Dynamic.ExpandoObject and Newtonsoft.Json.Linq.JObject
will help us more.


In the below example you can see how we can extract few data on the fly from a Type having all properties. This dynamic object example may help us developing web api where we need list.


Example:


using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Dynamic_Objects
{
    class Program
    {
 
        string GetCityAsJson()
        {
            City objCity = new City();
            var cities = objCity.GetAllCities().Select(o =>
            {
                dynamic city = new System.Dynamic.ExpandoObject();
                city.Text = o.Name;
                city.Value = o.ID;
                return city;
            }
            ).ToList();
 
            return JsonConvert.SerializeObject(cities);
        }
 
 
        string GetCityAsJson2()
        {
 
            City objCity = new City();
            var cities = objCity.GetAllCities().Select(o =>
            {
                dynamic city = new Newtonsoft.Json.Linq.JObject();
                city.Text = o.Name;
                city.Value = o.ID;
                return city;
            }
            ).ToList();
 
            return JsonConvert.SerializeObject(cities);
        }
 
 
        string GetCityAsJson3()
        {
 
            City objCity = new City();
            var cities = objCity.GetAllCities().Select(o =>
            new
            {
                Text = o.Name,
                Value = o.ID
            }
            ).ToList();
 
            return JsonConvert.SerializeObject(cities);
        }
 
        static void Main(string[] args)
        {
            Program obj = new Program();
            Console.WriteLine(obj.GetCityAsJson());
            Console.WriteLine("---------------------------------------");
            Console.WriteLine(obj.GetCityAsJson2());
 
            Console.Read();
        }
    }
 
 
 
    class City
    {
        public int ID { getset; }
        public string Name { getset; }
        public string PreviousName { getset; }
        public string ShortName { getset; }
        public decimal longitude { getset; }
        public decimal latitude { getset; }
 
        public List<City> GetAllCities() =>
            new List<City>(){
                new City() { ID= 1, Name = "New Delhi", PreviousName="Delhi", ShortName = "Del", latitude= 24.40m, longitude= 77.14m   },
                new City() { ID= 1, Name = "Tokyo", PreviousName="Tokyo", ShortName = "Tok", latitude= 35.40m, longitude= 139.45m   }
            };
 
    }
}


Tuesday, 15 May 2018

Linq CopyToDataTable extension for anonymous type IEnumerable

Casting Enumerable into List, Array is to common but sometime we also need to projection in to DataTable. Though there is one inbuilt CopyToDataTable<DataRow>() to get DataTable, but it only works with IEnumerable<DataRow>. Where as sometime we also required casting with anonymous type which we are getting through join or group by Linq query. Here I am providing both predefined and Custom Extension method which will solve our problem.

Predefined Snippet:


var allSubscriptions = from license in licensesDataTable.AsEnumerable()
                                          where license.Field<bool>("IsExpired") == true
                                          select license;
            DataTable expiredSubscriptions = allSubscriptions.CopyToDataTable<DataRow>();



Custom Extension Method:

public static class EnumerableExtensions
    {
        public static DataTable CopyToDataTable(this IEnumerable<object> data)
        {
            var parseDT = new DataTable();
            data.Select((r, i) => new { Key = i, Value = r }).ToList().ForEach(r =>
            {
                if (r.Key == 0)
                {
                    r.Value.GetType().GetProperties().ToList().ForEach(p =>
                    {
                        parseDT.Columns.Add(p.Name, p.PropertyType);
                    });
                }
                var row = parseDT.NewRow();
                r.Value.GetType().GetProperties().ToList().ForEach(p =>
                {
                    row[p.Name] = p.GetValue(r.Value, null);
                });
                parseDT.Rows.Add(row);
 
            });
            return parseDT;
        }
    }

Small Example:


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LinqGroupByIntoDataTable
{
 
    public static class EnumerableExtensions
    {
        public static DataTable CopyToDataTable(this IEnumerable<object> data)
        {
            var parseDT = new DataTable();
            data.Select((r, i) => new { Key = i, Value = r }).ToList().ForEach(r =>
            {
                if (r.Key == 0)
                {
                    r.Value.GetType().GetProperties().ToList().ForEach(p =>
                    {
                        parseDT.Columns.Add(p.Name, p.PropertyType);
                    });
                }
                var row = parseDT.NewRow();
                r.Value.GetType().GetProperties().ToList().ForEach(p =>
                {
                    row[p.Name] = p.GetValue(r.Value, null);
                });
                parseDT.Rows.Add(row);
 
            });
            return parseDT;
        }
    }
    class Program
    {
 
        static void Main(string[] args)
        {
            var dt = GetTable();
            var grp = from row in dt.AsEnumerable()
                      group row by row.Field<string>("City"into rowGroup
                      select new
                      {
                          City = rowGroup.Key,
                          Name = rowGroup.Min(r => r.Field<string>("Name")),
                          ID = rowGroup.Min(r => r.Field<int>("ID"))
                      };
 
 
            foreach (var key in grp)
 
            {
                Console.Write(key.City + " " + key.Name);
                Console.WriteLine();
            }
 
 
            var dtNew = grp.CopyToDataTable();
 
 
            Console.ReadKey();
        }
 
        static DataTable GetTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID"typeof(int));
            table.Columns.Add("City"typeof(string));
            table.Columns.Add("Name"typeof(string));
            table.Columns.Add("Date"typeof(DateTime));
 
            table.Rows.Add(25, "Delhi""Raj"DateTime.Now);
            table.Rows.Add(26, "Delhi""Suman"DateTime.Now);
            table.Rows.Add(10, "Pune""Ankur"DateTime.Now);
            table.Rows.Add(21, "Patna""Abhi"DateTime.Now);
            return table;
        }
 
 
    }
 
}

Output:

Tuesday, 8 May 2018

Micro-ORM Dapper Short Info

See what Microsoft says about Dapper in his official website.

Dapper is a micro-ORM built and maintained by StackExchange engineers. It focuses on performance, and can map the results of a query to a strongly-typed list, or to dynamic objects.

Dapper is in production use at Stack Overflow.

Supports

Dapper works with almost all popular provider of across all

  • .NET ADO providers
  • SQLite
  • SQL CE
  • Firebird
  • Oracle
  • MySQL
  • PostgreSQL and
  • SQL Server.

Advantages


  • Efficiently work with raw execution with minimal overheads.
  • Retain control over SQL server syntax.
  • Light wight ORM

Limitations

Implementation of Dapper use ConcurrentDictionary in order to maintain cache item for better performance of query execution.And the cached item never flushed. So if we use parameterised query than its good or else for the raw sql script execution it may hit memory.

My favorite
  • Multiple Results, Single execution with multiple query.
  • Multi Mapping, Work with Join.
  • Return list of dynamic objects, <dynamic>.
  • Support async call.
For examples have a look on StackExchange .


Tuesday, 17 April 2018

Table Hint NOLOCK In SQL Server

Resources on which lock apply:
  1. Pages: The fundamental unit of data storage in SQL Server is the page. The disk space allocated to a data file (.mdf or .ndf) in a database is logically divided into pages numbered contiguously from 0 to n. Disk I/O operations are performed at the page level.
  2. Extends: Extents are a collection of eight physically contiguous pages and are used to efficiently manage the pages. All pages are stored in extents.
  3. Table
  4. Partition
  5. Row

Types:
  1. Share Lock
    1. Whenever use Select statement
  2. Update Lock
    1. Whenever use Update statement
    2. Whenever use Delete statement
  3. Exclusive
    1. Update locks provide exclusive lock on objects so no other resources can use it.
  4. Intent
    1. Hierarchy based lock, and apply whenever we use perform operation on any hierarchical based objects (like: Row > Table> Page > Extend)
  5. Schema
    1. Whenever use DDL statement
    2. Types
      1. Schema m - one perform alter statement.
      2. Schema s - When execution plan creating.


Row level lock
  1. RID : Row Identifier, without index work with heap table.
  2. Key : Having Index.


Serial
SSMS Editor 1
SSMS Editor 2
Summary
1
CREATE TABLE dbo.Demo
(
    id INT IDENTITY NOT NULL,
    details VARCHAR(MAX) NULL
);

INSERT INTO dbo.Demo
(
    details
)
VALUES
('Record 1'),
('Record 2');

Just Created a table and inserted few records.
2

SELECT * FROM DB2016.dbo.Demo
No lock
3
BEGIN TRANSACTION TRAN1;
UPDATE dbo.Demo
SET details = 'Updated Record 2'
WHERE id = 2;


4

SELECT * FROM DB2016.dbo.Demo;

SELECT * FROM DB2016.dbo.Demo
 where id=1;
Locked, not able to get result. Because data lock applied on id=2 while searching one by one.
5

SELECT Top 5 * FROM DB2016.dbo.Demo WITH (NOLOCK)
Locked but still we are able to get data as explicitly  READUNCOMMITTED level.
6
ROLLBACK TRANSACTION


7

SELECT * FROM DB2016.dbo.Demo
Lock released and we got result.
8
CREATE CLUSTERED INDEX CID_ID ON dbo.Demo (id);

Index created.
9
BEGIN TRANSACTION TRAN1;
UPDATE dbo.Demo
SET details = 'Updated Record 2'
WHERE id = 2;


10

SELECT * FROM DB2016.dbo.Demo;
Locked because of id=2
11

SELECT * FROM DB2016.dbo.Demo
 where id=1;
Got result, because searched on index and id=1 is no locked.
12
COMMIT TRANSACTION;


13

SELECT * FROM DB2016.dbo.Demo;
Got data, lock released.

See what Microsoft and other say about uses of Table Hints like NOLOCK

Because the SQL Server query optimizer typically selects the best execution plan for a query, we recommend that hints be used only as a last resort by experienced developers and database administrators.

Support for use of the READUNCOMMITTED and NOLOCK hints in the FROM clause that apply to the target table of an UPDATE or DELETE statement will be removed in a future version of SQL Server. Avoid using these hints in this context in new development work, and plan to modify applications that currently use them.

Reference



Thursday, 12 April 2018

Simple In-memory implementation in C# using MemoryCache Class

In-Memory features, suitable for windows app and services. It also support change monitoring for its dependency. Available from .NET 4.0 +. We can store any type of object in cache.
Support for various callback like: On CacheEntryRemovedCallback, CacheEntryUpdateCallback.

By default available in asp.net. Reference http://www.asptricks.net/2016/12/aspnet-caching-technique-with-example.html

using System;
using System.Runtime.Caching;
using Newtonsoft.Json;
 
namespace MemCached
{
    class CacheItem
    {
        public string Data { getset; }
        public DateTime ModifiedOn { getset; }
        public string Key { getset; }
 
    }
 
    class Program
    {
 
        readonly static ObjectCache memoryCache;
        readonly static CacheItemPolicy policy;
        readonly static int minimumActiveDurationInSecond;
        private static CacheEntryRemovedCallback removeCallback = null;

 
        static Program()
        {
            minimumActiveDurationInSecond = 30;//max idle timeout 
 
            memoryCache = MemoryCache.Default; //Define default Region 
            policy = new CacheItemPolicy();
 
            //SlidingExpiration define max idle time for cache object based on uses.
            //We must set AbsoluteExpiration to DateTimeOffset.MaxValue if we use SlidingExpiration.
            policy.AbsoluteExpiration = DateTimeOffset.MaxValue;
            policy.SlidingExpiration = TimeSpan.FromSeconds(minimumActiveDurationInSecond);



            //RemovedCallback will call once Cached Item will be expired. 
            //We can use either CacheEntryUpdateCallback or CacheEntryRemovedCallback once. Other one should be null.
            removeCallback = new CacheEntryRemovedCallback(OnExpire);
            policy.RemovedCallback = removeCallback;
 
        }
 
 
 
        static void Main(string[] args)
        {
 
            ConsoleKeyInfo cki = new ConsoleKeyInfo();
 
            do
            {
 
                Console.WriteLine("Enter Key");
                var key = Console.ReadLine();
 
                CacheItem data = null;
                if (!memoryCache.Contains(key))
                {
                    Console.WriteLine($"Key [{key}] not found. Set value? Y/N");
                    if (Console.ReadLine().ToLower() == "y")
                    {
                        Console.WriteLine($"Set value for Key [{key}].");
                        data = createCacheData(key, Console.ReadLine());
                    }
                    else
                    {
                        continue;
                    }
 
                }
                else
                {
                    data = getCachedData(key);
                }
 
                if (data != null)
                {
                    Console.WriteLine("Data--------" + JsonConvert.SerializeObject(data));
 
 
                    Console.WriteLine("Do you want to change cached value? Y/N");
                    if (Console.ReadLine().ToLower() == "y")
                    {
                        Console.WriteLine($"Set value for Key [{key}].");
                        data = createCacheData(key, Console.ReadLine());
                    }
                    else
                    {
                        continue;
                    }
                }
 
 
                Console.WriteLine("Press the Escape (Esc) key to quit, Any key to continue.\n");
                cki = Console.ReadKey();
 
            } while (cki.Key != ConsoleKey.Escape);
 
        }
 
 
        /// <summary>
        /// Get Cache Item
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static CacheItem getCachedData(string key)
        {
            CacheItem data = null;
            key = key.ToLower();
            if (memoryCache.Contains(key))
            {
                var cacheItem = (CacheItem)memoryCache[key];
                data = cacheItem;
            }
            return data;
        }
 
        /// <summary>
        /// Make Entry in Cache with customized cache policy.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static CacheItem createCacheData(string key, string value)
        {
            var cacheItem = new CacheItem
            {
                Data = value,
                ModifiedOn = DateTime.Now,
                Key = key
            };
 
            if (memoryCache.Contains(key))
            {
                CacheItem source = (CacheItem)memoryCache.Get(key);
                CacheItem updated = cacheItem;
                memoryCache.Remove(key);
                memoryCache.Set(key, cacheItem, policy);
                OnUpdate(source, updated);
 
            }
            else
            {
                memoryCache.Set(key, cacheItem, policy);
            }
            return cacheItem;
        }
 
 
        public static void OnExpire(CacheEntryRemovedArguments cacheEntryRemovedArguments)
        {
            Console.WriteLine($"Key [{cacheEntryRemovedArguments.CacheItem.Key}] Expired. Reason: {cacheEntryRemovedArguments.RemovedReason.ToString()}");
        }
 
 
        public static void OnUpdate(CacheItem source, CacheItem updated)
        {
            Console.WriteLine($"Key [{source.Key}] Updated.");
            Console.WriteLine("Source--------" + JsonConvert.SerializeObject(source));
            Console.WriteLine("Updated--------" + JsonConvert.SerializeObject(updated));
        }
 
    }
}
 

Thursday, 15 March 2018

CSS for mathematical root or logarithm in html

Instead of using other CSS properties like, position, float and margin and padding we can use <SUP>
and <SUB> element in html in order to design any mathematical expression in html. have a look on given example with output.




 
 
<h2>Square root of n using SUP</h2>
2<sup class="up">2</sup> = 4 <br />
3<sup class="up">2</sup> = 9 <br />
4<sup class="up">2</sup> = 16 <br />
5<sup class="up">2</sup> = 25 <br />
 
 
<h2>Logarithm of n using SUB</h2>
log<sub>10</sub>1 = 0 <br />
log<sub>10</sub>10 = 1 <br />
log<sub>10</sub>100 = 2 <br />
log<sub>10</sub>1000 = 3 <br />
 
 
 
 
 
 
 
 
<hr />
 
<style>
    sup.up {
        vertical-alignsuper;
        font-sizesmaller;
    }
 
    sup.down {
        vertical-alignsub;
        font-sizesmaller;
    }
</style>
 
<h2>Logarithm of n using SUB and CSS</h2>
log<sup class="down">10</sup>1 = 0 <br />
log<sup class="down">10</sup>10 = 1 <br />
log<sup class="down">10</sup>100 = 2 <br />
log<sup class="down">10</sup>1000 = 3 <br />
 
 
 
<h2>Square root of n using SUB and CSS</h2>
2<sup>2</sup> = 4 <br />
3<sup>2</sup> = 9 <br />
4<sup>2</sup> = 16 <br />
 
 
 
 

How to join two Arrays in C#

A simple console application in which you can get how to join 2 arrays in C#. In this demo we have implemented multiple way to merge arrays.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayJoinDemo
{
    class Program
    {
        static void Main(string[] args)
        {
 
            int[] array1 = new int[] { 2, 1, 3 };
            int[] array2 = new int[] { 5, 4, 6 };
            Console.WriteLine("-----Method : 1--------------------");
            var join1 = array1.Union(array2).ToArray();
            Console.WriteLine(string.Join(",", join1));
 
            Console.WriteLine("-----Method : 2--------------------");
            var join2 = array1.Concat(array2).ToArray();
            Console.WriteLine(string.Join(",", join2));
 
            Console.WriteLine("-----Method : 3--------------------");
            int[] join3 = new int[array1.Length + array2.Length];
            Array.Copy(array1, join3, array1.Length);
            Array.Copy(array2, 0, join3, array1.Length, array2.Length);
            Console.WriteLine(string.Join(",", join3));
 
            Console.WriteLine("-----Method : 4--------------------");
            int[] join4 = new int[array1.Length + array2.Length];
            Buffer.BlockCopy(array1, 0, join4, 0, array1.Length * sizeof(int));
            Buffer.BlockCopy(array2, 0, join4, array1.Length * sizeof(int), array2.Length * sizeof(int));
            Console.WriteLine(string.Join(",", join4));
 
            Console.WriteLine("-----Method : 5--------------------");
            int[] join5 = new int[array1.Length + array2.Length];
            for (int i = 0; i < array1.Length; i++)
            {
                join5[i] = array1[i];
            }
            for (int i = 0; i < array2.Length; i++)
            {
                join5[array1.Length + i] = array2[i];
            }
            Console.WriteLine(string.Join(",", join5));
 
 
            Console.Read();
        }
    }
}


Output: