Thursday 29 September 2011

Extension methods

What are Extension Methods?

Extension methods allow you to extend an existing type with new functionality, without having to sub-class or recompile the old type. Extension Methods help blend the flexibility of "duck typing" support popular within dynamic languages today with the performace and compile-time validation of strongly-typed languages. This is a cool feature of C# 3.0

Extension Method Example:

Lets say you want to check if a variable string is a valid telephone number. In this example, our valid telephone number must start with a zero and followed by 9 more digits or start with a zero followed by two digits a space three digits a space and four digits (0787845122 or 078 784 5122). You would normally implement this by calling a separate class (most likely a static method) to check to see whether the string is valid, for instance:

string telephone = txtTelephone.Text;

if ( MyValidator.IsValidTelephone(telephone) ) {

}

Now using extension method, i can add a useful IsValidTelephone() method onto the string itself, which returns whether the string instance is a valid string or not. I can then re-write the above code to be cleaner and more informative like:






















Note that the static method above has a "this" keyword before the first parameter argument of type string. This tells the compiler that this particular Extension method should be added to objects of type "string". Within the IsValidTelephone() method implementation i can then access all of the public properties/methods/events of the actual string instance that the method is being called on, and return true/false depending on whether it is a valid telephone or not.
Hope you enjoyed this post, will add some example in the next post.

Thursday 15 September 2011

Linq equivalent to SQL Select Count and Group by

In this post i want to show you how you can do a select count and group by using LINQ that is equivalent to SQL's.
















We have our object Student and we are going to create a list of Students. Now we want to return a list of Students who are in a certain Class and the number of Students in that Class. To do that we are going to use the GroupBy method and use a lambda expression to return the group of Students in a Class as a list. From the list we can then select the Class name and the Count. Take note that I used an anonymous type in the Select method.




Now if we want to group by more than one field, for instance, Subject in this case here is how we can do it:


Results:



Hope you found this post useful. Like to hear from you for suggestions or questions.

What are lambda expressions

In this post i want to talk about lambda expressions. A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as "goes to". In other words, lambda expression can be broken down into parameters followed by execution code:

Parameter => executioncode.

The  left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block. The lambda expression x=> x * x  is read " x goes to x times x". In other words we want to square any given number and to do this we are going to assign to a delegate type as follows:



 







Result: The square of 5 is 25.

The => operator has the same precedence as assignment (=) and is right-associative.

Lambdas can be used in method-based LINQ queries as arguments to standard query operator methods such as Where. Let us look at the following pratical example:




























I have my object Person that i can model into a list of anything that represents a person, for instance, in this case i have modeled it to give me a list of Students. Now that we have our Students, we can go ahead and filter the Students by their Age, in this example i want to return teenagers or independents.

Both "getTeenagers()" and "getIndependents()" uses a lambda expression as a predicate to compare each Student's Age and return a  new collection of Students in their respective Age group.

For more on Lambda expressions you can read this article.

Thursday 1 September 2011

How to get a SharePoint list using SharePoint object model

In this post i want to discuss how you can get a SharePoint list using SharePoint object model. To do this make  sure you have SharePoint installed on your PC. Now create a new console project in Visual Studio 2010. Make sure that the platform target for your project is 64 bit and that you added the SharePoint dll as a reference.

Now, i have already added a custom list to my site and it is called My List. It has two columns, Name and Surname. So this is just a small list of random Names and Surnames.

 
 If you are done with your list, lets get the same list and display it from the Console application.

 If you run the code you will get the list you just created from SharePoint.


So this how you can simply get a SharePoint list using SharePoint object model.