class MyClass { public int age { get; set; } public string name { get; set; } } MyClass[] arr = { new MyClass() {age = 10, name ="abc" }, new MyClass() {age = 20, name ="xyz" }, new MyClass() {age = 30, name ="cde" } }; // create the parameter ParameterExpression param = Expression.Parameter(typeof(MyClass), "i"); // create the property to be used base on the // type (ie, MyClass) in the param variable. // you may change the "name" to any other field. Expression prop = Expression.Property(param, "name"); // construct the lambda expression. Expression> lambda = Expression.Lambda >(prop, param); // compile it to a function. Func compile = lambda.Compile(); // now, we can use the function by passing it // to the OrderBy() proc. var v3 = arr.OrderBy(compile); string s; foreach (var item in v3) { s = item.name; }
Wednesday, February 23, 2011
Dynamic sorting using Linq Expression
Thursday, December 30, 2010
WCF TCP Port Sharing
http://msdn.microsoft.com/en-us/library/ms734772.aspx
http://msdn.microsoft.com/en-us/library/ms731810.aspx
The article below has a diagram that depicts the port sharing:
http://www.codeproject.com/KB/WCF/Port_Sharing__in_WCF.aspx
Configuration:
http://blogs.msdn.com/b/drnick/archive/2006/04/14/configuring-http.aspx
Sunday, November 21, 2010
Open source Zip library
http://dotnetzip.codeplex.com/
Tuesday, October 26, 2010
Tuesday, October 19, 2010
Get the field size in the entity
In the entity framework, the diagram shown in the Visual Studio contains field information of all tables. You may get the field size that stores in the diagram by using the following method:
public static int GetMaxLength(ObjectContext p, string tb_name, string fld_name) { if (p == null) { throw new ArgumentNullException(); } MetadataWorkspace w = p.MetadataWorkspace; var items = w.GetItems(DataSpace.CSpace); //i.Properties["tb_name"].TypeUsage.Facets["MaxLength"].Value var v = items.Where(i => string.Compare(i.Name, tb_name, true) == 0); EntityType et = v.First(); object obj = et.Properties[fld_name].TypeUsage.Facets["MaxLength"].Value; if (obj != null) { string len = obj.ToString(); if (string.Compare(len, "max", true) == 0) { return -1; } else { int max_len = Int32.Parse(len); return max_len; } } else { return 0; } }
Linq literals
http://msdn.microsoft.com/en-us/library/bb399176.aspx
Saturday, October 16, 2010
Efficient way to add new parameter
It is so common that we need to add new parameter to the method during the system development or maintenance. Adding new parameter will become unavoidable when the business requirements changed.
For example, you have a method call Process which takes 2 integer parameters as shown below:
public int Process(int a, int b) { int result = a + b; return result; }
Now, what if you have to add a new integer parameter to this method? Normally, we will just simply add a parameter as we used to do. So, the new method will be look like this:
public int Process(int a, int b, int c) { int result = a + b + c; return result; }
Again, you have to add a new parameter (i.e., fourth parameter) due to the business process changed. OK. I think we need an efficient way to do this.
To avoid adding new parameter to the method (i.e., changing the method signature), we have to pass a struct or a object to the method. To do this,
1. Declare a class call MyParam:
public class MyParam { public int a { get; set; } public int b { get; set; } public int c { get; set; } }
2. Change the Process method to take in the MyParam class instead of the 3 parameters:
public int Process(MyParam p) { int result = p.a + p.b + p.c; return result; }
3. If you want to the fourth parameter, you have to add a new property instead of parameter. Also, the caller will no longer require to be modify because the method signature does not change.
The new MyParam class will be look like this:
public class MyParam { public int a { get; set; } public int b { get; set; } public int c { get; set; } public int d { get; set; } //<== new parameter. }
Of course, the implementation within the Process method must be modify and the caller must assign the value to property 'd' before passing it to the Process method.