Nov 7, 2022

Command to read Azure Redis Cache key value.

Here are the commands to retrieve key value:

  • If value is of type string - GET <key>
  • If value is of type hash - HGETALL <key>
  • If value is of type lists - lrange <key> <start> <end>
  • If value is of type sets - smembers <key>
  • If value is of type sorted sets - ZRANGEBYSCORE <key> <min> <max>
  • If value is of type stream - xread count <count> streams <key> <ID>

Use the TYPE command to check the type of value a key is mapping to:
  • type <key>

May 16, 2017

Example of generic and extension method in C#

namespace GenericSample
{
    public class DropdownListItem
    {
        public string Description { get; set; }
        public int Value { get; set; }
    }

    public class User
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
    }

    public class Role
    {
        public int RoleID { get; set; }
        public string RoleName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<User> users = new List<User>();
            users.Add(new User() { UserID = 1, UserName = "Hemant" });
            users.Add(new User() { UserID = 2, UserName = "Vijay" });

            List<Role> roles = new List<Role>();
            roles.Add(new Role() { RoleID = 1, RoleName = "Admin" });
            roles.Add(new Role() { RoleID = 2, RoleName = "Manager" });

            List<DropdownListItem> ddlUsers = users.ToSelectList<User>(u => u.UserName, u => u.UserID).ToList();

            Console.WriteLine("User Details:-");
            foreach (DropdownListItem ddl in ddlUsers)
            {
                Console.WriteLine("UserID: {0}, UserName : {1}", ddl.Value, ddl.Description);
            }
           
            List<DropdownListItem> ddlRoles = roles.ToSelectList<Role>(u => u.RoleName, u => u.RoleID).ToList();
            Console.WriteLine("Role Details:-");
            foreach (DropdownListItem ddl in ddlRoles)
            {
                Console.WriteLine("RoleID: {0}, RoleName : {1}", ddl.Value, ddl.Description);
            }
            Console.ReadLine();
        }
    }

    static class GenticClass
    {
        // Generic extension method to convert a type into DropDownItem
        public static List<DropdownListItem> ToSelectList<T>(this List<T> glist, Func<T, string> text, Func<T, int> value)
        {
            List<DropdownListItem> ddlList = new List<DropdownListItem>();
            glist.ForEach(x =>
            {
                ddlList.Add(new DropdownListItem() { Value = value(x), Description = text(x) });
            });
            return ddlList;
        }
    }
}


Mar 21, 2017

Plugin for String.format in javascript

//Helper functions
(function () {
    String.format = function () {
        // The string containing the format items (e.g. "{0}")
        // will and always has to be the first argument.
        var theString = arguments[0];

        // start with the second argument (i = 1)
        for (var i = 1; i < arguments.length; i++) {
            // "gm" = RegEx options for Global search (more than one instance)
            // and for Multiline search
            var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm");
            theString = theString.replace(regEx, arguments[i]);
        }
        return theString;
    }

})();

Jul 28, 2016

How to calculate the number of working hour between two dates except for the weekend?

Below solution will help you to find out the total number of working hours between two dates except for the weekend.

CREATE FUNCTION [dbo].[fnWorkTime]
(
    @StartDate DATETIME,
    @FinishDate DATETIME
)
RETURNS BIGINT
AS
BEGIN
    DECLARE @Temp BIGINT
    SET @Temp=0

    DECLARE @FirstDay DATE
    SET @FirstDay = CONVERT(DATE, @StartDate)

    DECLARE @LastDay DATE
    SET @LastDay = CONVERT(DATE, @FinishDate)

    DECLARE @StartTime TIME
    SET @StartTime = CONVERT(TIME, @StartDate)

    DECLARE @FinishTime TIME
    SET @FinishTime = CONVERT(TIME, @FinishDate)

    DECLARE @WorkStart TIME
    SET @WorkStart = '09:00'

    DECLARE @WorkFinish TIME
    SET @WorkFinish = '17:00'

    DECLARE @DailyWorkTime BIGINT
    SET @DailyWorkTime = DATEDIFF(MINUTE, @WorkStart, @WorkFinish)

    IF (@StartTime<@WorkStart)
    BEGIN
        SET @StartTime = @WorkStart
    END
    IF (@FinishTime>@WorkFinish)
    BEGIN
        SET @FinishTime=@WorkFinish
    END

    DECLARE @CurrentDate DATE
    SET @CurrentDate = @FirstDay
    DECLARE @LastDate DATE
    SET @LastDate = @LastDay

    WHILE(@CurrentDate<=@LastDate)
    BEGIN      
        IF (DATEPART(dw, @CurrentDate)!=1 AND DATEPART(dw, @CurrentDate)!=7)
        BEGIN
            IF (@CurrentDate!=@FirstDay) AND (@CurrentDate!=@LastDay)
            BEGIN
                SET @Temp = @Temp + @DailyWorkTime
            END
            --IF it starts at startdate and it finishes not this date find diff between work finish and start as minutes
            ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate!=@LastDay)
            BEGIN
                SET @Temp = @Temp + DATEDIFF(MINUTE, @StartTime, @WorkFinish)
            END

            ELSE IF (@CurrentDate!=@FirstDay) AND (@CurrentDate=@LastDay)
            BEGIN
                SET @Temp = @Temp + DATEDIFF(MINUTE, @WorkStart, @FinishTime)
            END
            --IF it starts and finishes in the same date
            ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate=@LastDay)
            BEGIN
                SET @Temp = DATEDIFF(MINUTE, @StartTime, @FinishTime)
            END
        END
        SET @CurrentDate = DATEADD(day, 1, @CurrentDate)
    END

    -- Return the result of the function
    IF @Temp < 0
    BEGIN
        SET @Temp = 0
    END
 RETURN @Temp
END


GO