Friday, August 21, 2015

I will be back soon

Hi All,
     I will be back with some important post soon ...keep reading
Thanks

Monday, September 16, 2013

How to use SqlClient.SqlFunctions.DateDiff in LINQ?

Here i am showing how to use SqlClient.SqlFunctions.DateDiff in Entity Framework to delete some records which are older than 8 hours.
/// <summary>
        /// Function Name: DeleteAllOutDatedSession
        /// Bishnu Tewary
        /// 16-09-2013
        /// This function will delete all those records which are older than 8 hours
        /// </summary>
        /// <param name="objEntities">DEMOEntities needs to be submitted to enable SQL-Transactions including Rollbacks</param>
        /// <returns> Result Type</returns>
        public DB DeleteAllOutDatedSession(DEMOEntities objEntities)
        {
            Result = DB.Unknown;
            try
            {
                using (objEntities)
                {
                   (from p in objEntities.tblDEMO.Where(x => System.Data.Objects.SqlClient.SqlFunctions.DateDiff("hour", x.Timestamp, DateTime.Now) >= 8) select p).ToList().ForEach(objEntities.DeleteObject);
                    objEntities.SaveChanges();
                }
                Report(DB.OK);
            }
            catch (Exception ex)
            {
                this.Report(DB.Error, ex);
            }
            return Result;
        }

Friday, September 13, 2013

How to replace multiple characters from a string in C#

 static Dictionary<char, char> ToReplace = new Dictionary<char, char>() { { '*', ' ' }, { '%', ' ' } };

Here ToReplace dictionary contains all characters and its replacement as key value , here the char you want to replace is KEY and the KEY will be Replace with VALUE from the bellow function

 /// <summary>
        /// Funtion Name: ReplaceWildchars
        /// Bishnu Tewary
        /// 13-09-2013
        /// This function will replace wildchar from the string
        /// </summary>
        /// <param name="inputString">input string</param>
        /// <returns>retun string after replacing </returns>
        public string ReplaceWildchars(string inputString)
        {
            var tmpStr = inputString.Select(c =>
            {
                char r;
                if (ToReplace.TryGetValue(c, out r))
                    return r;
                return c;
            });
            return new string(tmpStr.ToArray()).Trim();
        }