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.....
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 ::
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;
}
}
1 comment:
Its really helpful, thanks Bheeshm
Post a Comment