Pages

Sunday, March 10, 2013

Mother Teresa's Anyway Poem



People are often unreasonable, illogical and self centered;
Forgive them anyway.

If you are kind, people may accuse you of selfish, ulterior motives;
Be kind anyway.

If you are successful, you will win some false friends and some true enemies;
Succeed anyway.

If you are honest and frank, people may cheat you;
Be honest and frank anyway.

What you spend years building, someone could destroy overnight;
Build anyway.

If you find serenity and happiness, they may be jealous;
Be happy anyway.

The good you do today, people will often forget tomorrow;
Do good anyway.

Give the world the best you have, and it may never be enough;
Give the world the best you've got anyway.

You see, in the final analysis, it is between you and your God;
It was never between you and them anyway.

Saturday, March 9, 2013

Some Linq and NullableExtension

So, Here is some problem and solution going into My vault.

Problem1: I was having a Collection where each item has some sequence number, I need to override all the sequence number with my own sequence number starting from 1 to n , though not changing any repeated values.

Here i have done in two ways...First simple, another simpler via Linq. There are still possibility for optimizations, and i will keep looking into them always.....


static void Main(string[] args)
        {
            int _sequence=1;
            
            List<Employee> lst = new List<Employee>();
            lst.Add(new Employee { EmpId = 10, EmpName = "Bheeshm" });
            lst.Add(new Employee { EmpId = 11, EmpName = "Bheeshm1" });
            lst.Add(new Employee { EmpId = 12, EmpName = "Bheeshm2" });
            lst.Add(new Employee { EmpId = 13, EmpName = "Bheeshm3" });
            lst.Add(new Employee { EmpId = 10, EmpName = "Bheeshm4" });
            lst.Add(new Employee { EmpId = 10, EmpName = "Bheeshm5" });

            #region Region1
            //var a = lst.GroupBy(i => i.EmpId);

            //foreach (IGrouping<int, Employee> ii in a)
            //{
            //    foreach (Employee item in ii)
            //    {
            //        item.EmpId = _sequence;
            //    }
            //    _sequence++;
            //}

            //var orderedlist = lst.OrderBy(i => i.EmpId); 
            #endregion

            #region Region2
            lst.GroupBy(i => i.EmpId).ToList().ForEach(ii => { ii.ToList().ForEach(item => { item.EmpId = _sequence; }); _sequence++; });

           
            var orderedlist = lst.OrderBy(i => i.EmpId);
            #endregion
        }


Problem2: I was having two null-able values , and i requires to get the Max among them, It can be done easily but my quest was to do it with minimum number of code-lines.
System.Nullable provides some method, and i was trying to make an Extension method using the one provided by System.Nullable . But as Defined , System.Nullable was static , and i failed Doing so.
Then i received a very nice thought from an architect .... and here it is ::
    class Program
    {
        static void Main(string[] args)
        {
            
            DateTime? dt = null;
            DateTime? dt1 = DateTime.Now;

            DateTime? Max= NullableTypeExtension.ReturnMax(dt, dt1);
        }
    }

    public static class NullableTypeExtension
    {
        public static T? ReturnMax<T>(this T? n1, T? n2) where T : struct
        {
            int val = System.Nullable.Compare(n1, n2);
            if (val >= 0)
                return n1;
            return n2;
        }
    }