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:
- ICustomFunctionOperatorQueryable – which provides information about the method that will be associated with the custom function.
- 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
) 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:

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