Blogs

Gary's Blog

Data Layer – 11.1 Sneak Peek – Custom Functions Part 2

     

In a previous blog post I showed you how to use custom functions everywhere you could use a CriteriaOperator. In this post, I’m going to show you how to use custom functions in LINQ to XPO.

There are two interfaces that support this functionality, they are:

  1. ICustomFunctionOperatorQueryable – which provides information about the method that will be associated with the custom function.
  2. ICustomCriteriaOperatorQueryable – which provides information about the method that will be associated with custom criteria, and implements the behaviour to convert a LINQ expression to a CriteriaOperator.

Right, let’s write some code! To start with let’s open the project, from the previous post, in Visual Studio 2008 or greater, set target framework to 3.5 or greater and add a reference to the DevExpress.Xpo.v11.1.Linq assembly.

Next, in the MyConcatFunction class, add the implementation of ICustomFunctionOperatorQueryable

using DevExpress.Xpo.Helpers;
//...
public class MyConcatFunction : ICustomFunctionOperatorBrowsable, 
    ICustomFunctionOperatorFormattable, 
    ICustomFunctionOperatorQueryable 
{
    //The method name must be the same as the function name,
    //but it doesn't have to be in the same class
    public static string MyConcat(string string0, string string1, string string2, string string3) {
        return string.Concat(string0, string1, string2, string3);
    }
    #region ICustomFunctionOperatorQueryable Members
     
    public System.Reflection.MethodInfo GetMethodInfo() {
        return typeof(MyConcatFunction).GetMethod("MyConcat");
    }
    #endregion
}

Finally we will drop another GridControl (imaginatively called GridControl2 Smile) onto the form and add the following code to the form constructor:

var linqResult = from p in new XPQuery<Person>(Session.DefaultSession)
                 select new
                 {
                     p.FirstName,
                     p.LastName,
                     NameLinqToXpo =
                         MyConcatFunction.MyConcat(p.FirstName,
                         " ", 
                         p.LastName, 
                         " (Linq To Xpo)")
                 };
gridControl2.DataSource = linqResult.ToList();

Having done that, running the application will give us this result:

Example Form showing custome functions in LINQ to XPO

That’s all for this short post, until next time, happy coding! Smile

Published Mar 23 2011, 05:05 PM by Gary Short (DevExpress)
Filed under:
Technorati tags: v2011.1 Sneak Peek
Bookmark and Share

Comments

No Comments
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.