Category Archives: LINQ

F# “fun” with Windows Phone 7


Don syme announced the availability of F# for Windows Phone 7 (link). Daniel Mohl has a project template for F# apps that Don showed in the F# community demo (link).

Note: Anyone wanting to start off a project from scratch can download the project template that I created from here. This is a simple F# WP7 app template without the extra demo files.

As with my previous posts (long back, can’t find enough time these days), I ported the Stock ticker code base to WP7. The UI is pretty simple, It only has a ListBox to show the stock data. The ListBox has a custom ItemTemplate that defines the layout for the input data pulled over the web (Yahoo finance). The application also checks for network connectivity. Below is a snapshot,


This sample only pulls the stocks when you click the button, Since it is used in a phone, actual CEP kind of scenario would not be suitable, so keeping the updates in deferred mode. UI polishing + added features would make this a good app for the market store, what do you think? 😉

Sample with source code link.



CEP Client – Remix (WPF & SL)

My previous post had implementation of Rx + F# + Syncfusion WPF GridDataControl. This post is a continuation with a cleaner sample + a Silverlight 4 client using the same abstractions. The technologies used are,

· Rx (, Download the latest version for Silverlight.

· F# – ( Get the latest SL4 tools for VS2010, it includes a copy of Fsharp.Core.dll built for the SL 4 Framework.

The sample code is much the same; it uses code sharing between the WPF / SL source projects with some conditional compilation constants to make it build properly. One more caveat is that, since we doing a Webrequest call over the network, we need elevated permissions to do in a Silverlight application, without this we would have to host a client access policy file in the server. Only way is to run the SL application in an elevated trust.

Silverlight 4 – Out Of Browser application,


You would have to send me an email again to get the sample code. May be if I get more time, I can work on this and make it colorful to host it in our site itself :).


Embedding LINQ Expression for a method call

Consider evaluating an Expression at runtime, for some lame reason you had to call ToLower() on a string object, and create a predicate out of it. Using LINQ expressions simplifies the task of doing these things. Simply embed the custom Expression that would evaluate against the LINQ extension methods. Below is a sample MethodCallExpression to embed a ToLower() method inside a LINQ query,

private static MethodCallExpression GetToLowerMethodCallExpression(Expression memExp)
    var tolowerMethod = typeof(string).GetMethods().Where(m => m.Name == "ToLower").FirstOrDefault();
    var toLowerMethodCall = Expression.Call(
        new Expression[0]);
    return toLowerMethodCall;

Lets take a simple scenario, we would write a custom Expression (e) => some lambda functor, that would evaluate against a Contains predicate. We first would right down a normal functor that would return the value of a property by using a PropertyDescriptor,

Func<string, object> recordFunc = (columnName) =>
    var pd = properties.Find(columnName, false);
    return pd.GetValue(person1);

Since we cannot embed a full method inside an expression we split that into a Functor lambda, and then use that as a single expression given below,

Expression<Func<Person, bool>> e = (p) => p.Age1 == (int)recordFunc("Age2");

C# compiler generates a sequence of Expression calls to the lambda recordFunc (take a look in reflector with the compiled exe),

       Expression<Func<Person, bool>> e = Expression.Lambda<Func<Person, bool>>(Expression.Equal(Expression.Property(CS$0$0000 = Expression.Parameter(typeof(Person), "p"), (MethodInfo) methodof(Person.get_Age1)), Expression.Convert(Expression.Invoke(Expression.Constant(recordFunc), new Expression[] { Expression.Constant("Age2", typeof(string)) }), typeof(int))), new ParameterExpression[] { CS$0$0000 });

If we split this above expression into more meaningful method call, it should be as below,

public static LambdaExpression Contains(this object source, string propertyName, string propertyName2, Expression<Func<string, object>> recordFunctor)
    var type = source.GetType();
    var paramExp = Expression.Parameter(type, type.Name);
    var member = Expression.PropertyOrField(paramExp, propertyName);
    var member2 = Expression.PropertyOrField(paramExp, propertyName2);
    var convertedExp = Expression.Convert(Expression.Invoke(Expression.Constant(recordFunctor), new Expression[] { Expression.Constant(propertyName2, typeof(string)) }), member.Type);
    var binaryExp = Expression.Equal(member, convertedExp);
    var lambda = Expression.Lambda(binaryExp, paramExp);
    return lambda;

We can then use the above Contains extension method to embed a normal expression into LINQ predicate syntax or any LINQ extension method that requires a BinaryExpression to execute.

Download the sample here.