Inline conversion/cast from Expression> to LambdaExpression I have the following code (I need the LambdaExpression as not all Func&lt;&gt;'s are T, int):

var orderDelegates = new Dictionary&lt;string, LambdaExpression&gt;();

Expression&lt;Func&lt;Image, int&gt;&gt; id = i =&gt; i.Id;
orderDelegates.Add(ContentItem.ORDER_BY_ID, id);


Is it possible to make this shorter?

Best something like:

var orderDelegates = new Dictionary&lt;string, LambdaExpression&gt;()
{
    { ContentItem.ORDER_BY_ID, ????? },
    ...
};


I already tried the following, which did not work:

orderDelegates.Add(ContentItem.ORDER_BY_ID, (Expression&lt;Func&lt;Image, int&gt;&gt;)i =&gt; i.id);
//or
orderDelegates.Add(ContentItem.ORDER_BY_ID, i =&gt; i.id as Expression&lt;Func&lt;Image, int&gt;&gt;);


I "improved" it a little to the following already, but "full inlining" would be the desired result:

public static Dictionary&lt;string, LambdaExpression&gt; GetOrderDelegates&lt;T&gt;() where T : ContentItem
{
    Expression&lt;Func&lt;T, int&gt;&gt; id = i =&gt; i.Id;
    Expression&lt;Func&lt;T, bool&gt;&gt; enabled = i =&gt; i.IsEnabled;
    Expression&lt;Func&lt;T, IComparable&gt;&gt; author = i =&gt; i.Creator.UserName;
    //...

    return new Dictionary&lt;string, LambdaExpression&gt;()
        {
            { ContentItem.ORDER_BY_ID, id },
            { ContentItem.ORDER_BY_ENABLED, enabled },
            { ContentItem.ORDER_BY_AUTHOR, author },
            //...
        };
}
You can do this by creating a custom class that supports collection initializers. Its Add() method would be generic, so that it fits any allowed expression:

class ExpressionDictionary&lt;T&gt; : IEnumerable&lt;KeyValuePair&lt;string, LambdaExpression&gt;&gt;
{
    private readonly Dictionary&lt;string, LambdaExpression&gt; m_dictionary
        = new Dictionary&lt;string, LambdaExpression&gt;();

    public void Add&lt;TResult&gt;(string key, Expression&lt;Func&lt;T, TResult&gt;&gt; expression)
    {
        m_dictionary.Add(key, expression);
    }

    public IEnumerator&lt;KeyValuePair&lt;string, LambdaExpression&gt;&gt; GetEnumerator()
    {
        return m_dictionary.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}


Usage:

new ExpressionDictionary&lt;T&gt;
{
    { ContentItem.ORDER_BY_ID, i =&gt; i.Id },
    { ContentItem.ORDER_BY_ENABLED, i =&gt; i.IsEnabled },
    //the cast is necessary if the result has to be Func&lt;T, IComparable&gt;
    // and not Func&lt;T, string&gt;
    { ContentItem.ORDER_BY_AUTHOR, i =&gt; (IComparable)i.Creator.UserName },
    //...
};


You will need to add other methods to ExpressionDictionary. Either implement the whole IDictionary&lt;TKey, TValue&gt; interface and make GetOrderDelegates() return that. Or just add a getter for the private Dictionary and return that.

But even better option might be not to use Dictionary at all. Instead create OrderByDeletegates class (though that's probably not the best name, since it would contain expressions, not delegates) that would have properties for all expressions:

class OrderByDeletegates&lt;T&gt; where T : ContentItem
{
    public Expression&lt;Func&lt;T, int&gt;&gt; OrderById { get; set; }
    public Expression&lt;Func&lt;T, bool&gt;&gt; OrderByEnabled { get; set; }
    public Expression&lt;Func&lt;T, IComparable&gt;&gt; OrderByAuthor { get; set; }
    // …
}


Usage:

new OrderByDeletegates&lt;T&gt;
{
    OrderById = i =&gt; i.Id,
    OrderByEnabled = i =&gt; i.IsEnabled,
    OrderByAuthor = i =&gt; i.Creator.UserName,
    // …
};


The advantage of this is that it's type-safe. You can't for example accidentally do something like OrderByEnabled = i =&gt; i.Id.