Lambda expressions and anonymous methods on Metadata service in CRM 4.0

In C# 3.0 Microsoft has added lambda expression. This is not a new feature in computer languages originally designed on a conceptualized level way back in time by an American called Alonzo Church.

Lamda expressions provide shorthand syntax for algorithms.

I have played around with this and below is a simple example where I retrieve all entities from the CRM metadata service. I call my filter method and provide my delegate with the filter expression. I have also an example on using anonym’s method, in previous versions before 2.0 you had to write a named method and pass that name where the delegate was required. The syntax for C# operator is => and lambda expression. Example is simple and I'm only using one param1 (filter) and checks if entity is customizable.

static void Main(string[] args)


//Login and retrive all entities from metadataservice (change name to your org)

RetrieveAllEntitiesResponse allEntitiesResponse = Metadata.RetriveAll("mydemo");

//Lambda expression

EntityMetadata[] filterEntities = Common.FilterMetadata(allEntitiesResponse,

filter => (filter.IsCustomizable.Value == true)



//Anonymous method

EntityMetadata[] filterEntities = Common.FilterMetadata(allEntitiesResponse,

delegate(EntityMetadata filter)


return (filter.IsCustomizable.Value == false);



//Write result to console

int total = 0;

foreach (EntityMetadata item in filterEntities)





Console.WriteLine("Return of record(s) {0}", total);




If your filter is complex you might consider to create a named method.

I have attached Visual Studio 2008 project for you to play around with.

Lambda on MSDN

This posting is provided "AS IS" with no warranties, and confers no rights.