Lambda Utilities to get nested property name and value

It is handy to develop code that does not use directly the string name of a property when creating method to work against dynamic code. In the past, we would wrote code like that:

bool isNotNull = MyMethod(myObjectToVerify, "propertyToVerify");

The problem with code that specify string method’s name is that error occurs. You can mistype, you can forgot to rename the string when refactoring or you can simply not know about that string when during a replace this one got changed without you even knowing. This is why, since Microsoft .Net has introduced Lambda, you should use an expression. The result is strongly typed and it looks like this:

bool isNotNull = LambdaUtilities.VerifyNotNull( ()=>myObjectToVerify.propertyToVerify );

The code to verify the value is very simple. It consists of compiling the expression and invoking the property. If this one return null, return false, otherwise, return true. I am not going to show you how to create that simple true/false condition but how to get the value.

public static object GetValue<T>(Expression<Func<T>> selector)
{
    var method = selector.Compile();
    var value= method.Invoke();
    return value;
}

The method works for nested property. So you can use these variants when using it:

var x =  GetValue(()=>yourObject.Property);
var x2 = GetValue(()=>yourObject.Property.NestedProperty);
var x3 = GetValue(()=>yourObject.Property.NestedProperty.AndSoOn);

Getting the name of the property require more code. The code come from one of my project and if my memory is good, come almost all from a StackOverflow post.

public static string GetPropertyName<T>(Expression<Func<T>> expression)
{
	var stack = new Stack<string>();
	Expression expression1 = expression.Body;
	while (expression1 != null)
	{
		if (expression1.NodeType == ExpressionType.Call)
		{
			var methodCallExpression = (MethodCallExpression)expression1;
			if (IsSingleArgumentIndexer(methodCallExpression))
			{
				stack.Push(string.Empty);
				expression1 = methodCallExpression.Object;
			}
			else
				break;
		}
		else if (expression1.NodeType == ExpressionType.ArrayIndex)
		{
			var binaryExpression = (BinaryExpression)expression1;
			stack.Push(string.Empty);
			expression1 = binaryExpression.Left;
		}
		else if (expression1.NodeType == ExpressionType.MemberAccess)
		{
			var memberExpression = (MemberExpression)expression1;
			stack.Push("." + memberExpression.Member.Name);
			expression1 = memberExpression.Expression;
		}
		else if (expression1.NodeType == ExpressionType.Parameter)
		{
			stack.Push(string.Empty);
			expression1 = null;
		}
		else if (expression1.NodeType == ExpressionType.Convert)
		{
			var memberExp = ((UnaryExpression)expression1).Operand as MemberExpression;
			stack.Push("." + memberExp.Member.Name);
			expression1 = memberExp.Expression;
		}
		else
			break;
	}
	if (stack.Count > 0 && string.Equals(stack.Peek(), ".model", StringComparison.OrdinalIgnoreCase))
		stack.Pop();
	if (stack.Count <= 0)
		return string.Empty;
	return (stack).Aggregate(((left, right) => left + right)).TrimStart(new[] { '.' });
}

private static bool IsSingleArgumentIndexer(Expression expression)
{
	var methodExpression = expression as MethodCallExpression;
	if (methodExpression == null || methodExpression.Arguments.Count != 1)
		return false;
	return (methodExpression.Method.DeclaringType.GetDefaultMembers()).OfType<PropertyInfo>().Any((p => p.GetGetMethod() == methodExpression.Method));
}

To get the name of the property, you have to call it like the one to get the value. This one support as well nested properties.

var x =  GetPropertyName(()=>yourObject.Property);
var x2 = GetPropertyName(()=>yourObject.Property.NestedProperty);
var x3 = GetPropertyName(()=>yourObject.Property.NestedProperty.AndSoOn);

With these two methods, you should be in business to get the value and the name of properties from Lambda. Avoiding the use of string is interesting for your maintainability and it allows you to modify without fearing to forget a string somewhere.

KeyValuePair does not return NULL with Linq to Object SingleOrDefault Method

If you have a list of key value pair and you are searching something that might not be there, you may want to use SingleOrDefault, or FirstOrDefault to get this element. If it does not exist, you may think that the Linq to object return null but in fact, it return the default value which is a new instance of KeyValuePair class.

    var kvp1 = new KeyValuePair<string, string>("a", "b");
    var kvp2 = new KeyValuePair<string, string>("c", "d");
    var list = new List<KeyValuePair<string, string>> {kvp1, kvp2};
    var value = list.SingleOrDefault(d => d.Key == "notfound").Value;

The code above return from SingleOrDefault a new KeyValuePair object with the Key and the Value to NULL. The return of the Linq is not NULL.

In fact, this is the case of any of your classes that you search and that this one is not found.

var kvp3 = new MyKeyValuePair {Key = "a", Value = "b"};
var kvp4 = new MyKeyValuePair {Key = "c", Value = "d"};
var list2 = new List<MyKeyValuePair> { kvp3, kvp4 };
var value2 = list.SingleOrDefault(d => d.Key == "notfound").Value;

public class MyKeyValuePair
{
    public string Key { get; set; }
    public string Value { get; set; }
}

The result is that value2 is an Exception and this is because SingleOrDefault has returned NULL. How come? It returns the default value has the name of the method specify. So, if we verify the default value of a class we will get an empty object right? Wrong! We are getting a NULL.

var defaultIs = default(MyKeyValuePair); //This return null!

If we check the source code of SingleOrDefault, we realize that it uses the exact same default method.

public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {
	if (source == null) throw Error.ArgumentNull("source");
	if (predicate == null) throw Error.ArgumentNull("predicate");
	TSource result = default(TSource);
	long count = 0;
	foreach (TSource element in source) {
		if (predicate(element)) {
			result = element;
			checked { count++; }
		}
	}
	switch (count) {
		case 0: return default(TSource);
		case 1: return result;
	}
	throw Error.MoreThanOneMatch();
}

KeyValuePair class, or should I say the KeyValuePair struct default is different. The reason is that the default value of a structure is not the same as a class. It returns an new structure and not null. The mystery is now resolved. For your information, you cannot define you “default value” for your classes. Here are something interesting from MSDN.

The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference type.

Entity Framework SELECT VALUE Q with ESQL, why?

When you are using Entity Framework and want to pass a query string manually, you have to use SELECT VALUE XXX FROM … why? This is a good question and often we see SELECT VALUE Q FROM, why Q? In fact, it can be anything but a single word. ESQL allow to do a select with a row wrapper or without. When using VALUE, it adds a wrapper which create a return of a materialized data record. Entity Framework handle this materialized data record to bind the result into a context object. Without the VALUE, you do not have any wrapper and you get back a set of rows.

Here is an example with SELECT VALUE. We receive a strongly typed set of object.

string queryString = "SELECT VALUE q from table1.attr1 as q";
ObjectQuery<T> query = context.CreateQuery<T>(queryString);

You can also specify which field you return if you do not want every fields.

string queryString = "SELECT VALUE row (q.Field1 as Field1, q.Field2 as Field2) from table1.attr1 as q)";
ObjectQuery<T> query = context.CreateQuery<T>(queryString);

This time, the keyword “row” is required because it’s a reserved keyword by ESQL. ROW constructs an anonymous value.

And an example that return a set of rows. As you can see, we have a DbDataRecord.

string queryString = "SELECT q table1.attr1 as q";
ObjectQuery<DbDataRecord> query = context.CreateQuery<DbDataRecord>(queryString);

Most of the time, you will use the SELECT VALUE q FROM … The use of ESQL must be as low as you can because it opens a door to have SQL Query inside your code, which the ORM is there to abstract. Nevertheless, sometime, for optimization, ESQL is perfect.

How to display a list by category with Linq and Grouping

If you are using Asp.Net MVC, you could have a set of entities which are going to be mapped from your model to your view model. Here is a sample of a situation where we have an exercise that is related to a muscle. It’s a 1 to n relationship where the exercise are linked to a single muscle, but the muscle could be trained by many exercises.

MuscleAndExercise

As you can see, the mapping between the view model and the model is simple, the side of the exercise, where only one instance of muscle could be attached, is merged into the view model of the exercise. This one is called ExerciseViewModel in the following class diagram.

ExerciseViewModel

So, if you want to display a list of exercise you can get the whole list by getting from your database context the list of exercise. Here is the partial view that get a list of ExerciseViewModel and dress a list of all exercises inside an Html list.

@model IEnumerable<ViewModels.ExerciseViewModel>
<ul class="exercises-container-source">
@foreach (var exercise in Model)
{      
	<li data-exercise-id="@exercise.Id">
		<span class="exerciseName">@exercise.Name</span>
	</li>
}    
</ul>

Let say that now you want to display exercises by their muscle group. This is not a problem from the class point of view because the ExerciseViewModel contain directly the MuscleId and MuscleName.

The “old school” way to do it would be to sort the collection by the exercise name, then to loop through the collection and every time the muscle group change, to write the muscle name.

@model IEnumerable<ViewModels.ExerciseViewModel>
<ul class="exercises-container-source">
int lastId = -1;
@foreach (var exercise in Model.OrderBy(d=>d.MuscleId))
{      
	if(lastId != exerciseMuscleId)
	{
		if(lastId != -1)
		{
			</ul>
		}
		<h3>@exercise.MuscleName</h3>
		<ul class="exercises-container-source">
		lastId = exerciseMuscleId;
	}
	<li data-exercise-id="@exercise.Id">
		<span class="exerciseName">@exercise.Name</span>
	</li>
}    
</ul>

This kind of code is not particularly great and invokes some logic to understand it correctly. First, we have to handle the last muscle id into a variable and change its value depending of a comparison. Second, we have to handle the creation of the Html tag “UL” because header tag (h3) cannot be inserted between “UL” tag. This mean that we need to open and close the “UL” tag every time we have an exercise.

With Linq and Grouping we can simplify by a lot this whole process. The main advantage of the next methodology is that without modifying any backend classes the view page will be cleaner and easier to understand.

@model IEnumerable<ViewModels.ExerciseViewModel>   
@foreach (var exerciseGroup in Model.GroupBy(d => new { Id = d.MuscleId, Name = d.MuscleName }))
{
    <h3>@exerciseGroup.Key.Name</h3>
    <ul class="exercises-container-source">
    @foreach (var exercise in exerciseGroup)
    {
        <li data-exercise-id="@exercise.Id">
            <span class="exerciseName">@exercise.Name</span>
        </li>
    }
    </ul>
}

Here you go. No more integer to know that last exercise, no anymore weird handling of the “UL”. It’s very clear so the maintainability of the code is easier. As you can see, we first group every exercise by MuscleId and MuscleName. We need to have both because we want to show the name. As you can see, we also display the name which is part of the key. With Visual Studio 2010 and 2012, your anonymous type will be handled by Microsoft Intellisense and you will see that your Key’s property have the Id and the Name properties. After displaying the muscle name, you just need to loop through the collection of exercise and display the exercise name.

I hope that you will be able to use the Linq Grouping functionality in you application more often because it can help greatly simple task to be cleaner.

Linq Aggregate to concatenate string with a comma

If you have an array that you want to flatten into a string with a comma between each entry, you could use Linq with a one liner delegate to reach this goal.

string flatten = inputs.Aggregate((current, next) => 
                                 string.Format("{0}, {1}", current, next))

This is quite powerful as you can see, you do not have to do validation to know if you have reach the last entry to not add a trailing comma. Without Linq and the aggregate function, you would have to loop and to verify this condition.

string flatten = string.Empty;
for(int i = 0 ; i < inputs.Length ; i++)
{
   if(i!=(i.Length-1))
   {
       flatten += str + ", ";
   }
   else
   {
       flatten += str;
   }
}

I don’t know for you, but I found more easy to read the Aggregate method than the second snippet of code.

Linq and grouping with custom object

Depending of what you want to do, you may want to group by id or by more values. A common case is to group by a unique idenfier but also want to get some connexe information like let say having the name. For example, if we want to group every jobs that every body had in their life, we would like to group by userid. But we may want to have their full name.

var groupedList = nonGroupedList.GroupBy(g=> new MyCustomGroupingClass(g.UserId, g.UserFullName));

This won’t work because every MyCustomGroupingClass will be different from C# perspective. This is because Linq will compare every object and figure out that every MyCustomGroupingClass has a different object.

var groupedList = nonGroupedList.GroupBy(g=> g.UserId);

This would work, because an integer is comparable but it doesn’t solve the need to have the user name.

The solution is hidden in the problem : comparing classes. We need to provide a way to compare. This can be done by providing an override to the Equals method of your grouping class.

public class MyCustomGroupingClass
{
	public int ID { get; set; }
	public string FullName { get; set; }

	public override bool Equals(object obj)
	{
		if (ReferenceEquals(null, obj)) return false;
		if (ReferenceEquals(this, obj)) return true;
		if (obj.GetType() != this.GetType()) return false;
		return Equals((MyCustomGroupingClass)obj);
	}
	protected bool Equals(MyCustomGroupingClass other)
	{
		return ID.Equals(other.ID) && string.Equals(FullName, other.FullName);
	}
}

From here, you will be able to group without problem with Linq and your custom grouping class.

How to extend (or add) conditionnal clause to Expression

If you need to concatenate two expressions (with OR or AND) this can be done easily with the help of the LinqKit library.

The idea is to Invoke both expression with the same model object.

Expression<Func<Customer, bool>> filter1 = customer => customer.FirstName == "Test";
Expression<Func<Customer, bool>> filter2 = customer => customer.LastName == "Test";
Expression<Func<Customer, bool>> filter3 = customer => filter1 .Invoke(customer) || filter2.Invoke(customer);

The last step is to use AsExpendable() and to you Expand to the last filter (filter3).

IEnumerable<Customer> customers = CustomerRepository.GetAll().AsExpandable().Where(filter3.Expand()).OrderBy(c => c.Id);

The AsExpendable() method simple create a wrapper around the IQueryable to create a ExpendableQuery. From here, the provider will change with the concrete provider ExpendableQueryProvider which inherit from IQueryProvider. This one will call the .Expand() of the expression.

If you go check the source code of the method ExpendableQuery we can see the wrapping.

public static IQueryable<T> AsExpandable<T>(this IQueryable<T> query)
{
  if (query is ExpandableQuery<T>)
	return query;
  else
	return (IQueryable<T>) new ExpandableQuery<T>(query);
}

The expand look like this :

    public static Expression<TDelegate> Expand<TDelegate>(this Expression<TDelegate> expr)
    {
      return (Expression<TDelegate>) new ExpressionExpander().Visit((Expression) expr);
    }

It call the Visit method of the ExpressionExpander from the Expression.

In short, what you have to remember it the use of LinqKit.dll make you life really easier when you manipulate dynamic expression.

So you have appended two Linq To Entity expression
http://stackoverflow.com/questions/1266742/append-to-an-expression-linq-c

How to use Linq to query a property that is not in your database but in your class model

One of my team mate would like to query some data with Linq but has always an error message saying that the Linq to Entity couldn’t find the property desired. In fact, the exception was relevant because the property was made up in the model and were using two others properties available from the database.

To figure out more about the problematic let say that in the database you have the field : Id, Name_Fr, Name_En. Let say that in your model you have : Id, Name_Fr, Name_En and also Name. You would like to sort data by name. If you use directly your Entity Framework’s data context to query with Linq To Entity you will get an error because database context will try to do a SQL query with the field Name which doesn’t exist. The solution is to use Linq to Entity to query everything and to sort with Linq to Object.

So, instead of doing :

MyDatabaseContext.MyPeople.OrderBy(c=>c.Name);

You would have to do :

MyDatabaseContext.MyPeople.AsEnumerable().OrderBy(c=>c.Name);

The AsEnumerable() will execute the query which will return a collection of IEnumerable instead of IQueryable. The difference is that MyPeople is IQueryable which doesn’t execute the query until the collection is enumerated (with ForEach for example) or until the query it transformed into a list (for example with ToList()).

The difference between AsEnumerable() and ToList() is concerning performance. The AsEnumerable() is faster if you do subsequent filtering (.Where for example) operation. The reason is that it will filter the query before looping instead of looping the whole collection and then looping to filter.

Access to modified closure when having a Linq inside a loop

If you have Resharper, you can see sometime a warning saying that you “Access to modified closure”. If you do not have it, you can still have odd behavior something with the values. In both case, this mean that Linq is accessing a value that might (or not) be modified.

Linq execute delegate functions. If you try :

var  listInteger = new List<int>() { 1, 2, 3, 4, 5 }; //Values

var funcs = new List<Func<int>>(); //Delegate function that take a Integer as parameter

foreach(var v in listInteger)  // Add in the list of delegate's functions a function that return the integer value
{
	funcs.Add( ()=>v );
}
	
foreach(var f in funcs)  //Execute functions which should just return the value.
{
	Console.WriteLine(f()); // We expect here to have 1,2,3,4,5
}

You wull not see 1,2,3,4,5 but 5,5,5,5,5. The reason is that is access a modified variable. The function is not adding the value of v but the pointer to it. This is why it remains on the last value of the loop. But, by using a variable inside the loop, the pointer is to this variable and directly to the good value.

var  listInteger = new List<int>() { 1, 2, 3, 4, 5 }; //Values

var funcs = new List<Func<int>>(); //Delegate function that take a Integer as parameter

foreach(var v in listInteger)  // Add in the list of delegate's functions a function that return the integer value
{
	var vv = v;
	funcs.Add( ()=>vv );
}
	
foreach(var f in funcs)  //Execute functions which should just return the value.
{
	Console.WriteLine(f()); // We expect here to have 1,2,3,4,5
}

Most of the time, no body will feel this problem because most of the time the Linq won’t execute delegate function defined somewhere else but an anonymous function that call directly the value of the variable. But, as a good practice, it’s always better when accessing variable that may be changed to create a temporary variable to remove the undesired effect.

How to compare element from the same collection with Linq?

Lets imagine that you have a collection with a class containing Images. All images are identified by an unique key and a caption that the user enter. You want to verify that the caption entered by the user is unique to the collection. How to do it?

This can be done with Linq to Object pretty easily by using 2 from statement.

var hasDouble =  
         (from s1 in this.Images
          from s2 in this.Images
          where s1.Key != s2.Key
                && s1.Caption == s2.Caption
         select new { Image1 = s1, Image2 = s2 }).Any();

This will loop through all the images and will compare the caption of all shape but not compare node with itself.
I have created an anonymous object because in fact, I may want to get those 2 elements that have the same caption. In that case I just need to call the same code and remove the .Any().

This can be translated to Lambda expression.

(this.Shapes.SelectMany(s1 => this.Images, (s1, s2) => new{s1, s2})
             .Where(tt => tt.s1.Key != tt.s2.Key
              && tt.s1.Caption == tt.s2.Caption)
             .Select(tt => new { Image1 = tt.s1, Image2 = tt.s2}
)

But for this kind of task, Lambda is more confusing from my point of view.