예제 #1
0
        public static string GetMemberName(this LambdaExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            try
            {
                string memberName = expression.Coalesce(
                    e => (e.Body as MemberExpression).ValueOrDefault(m => m.Member.Name),
                    e => (e.Body as UnaryExpression).ValueOrDefault(u => (u.Operand as MemberExpression).ValueOrDefault(m => m.Member.Name)));

                if (memberName == null)
                {
                    throw new InvalidOperationException("Could not determine the member name from the expression.");
                }

                return(memberName);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Could not determine the column name from the expression. In order to automatically determine the column name, the selector must be simple, for instance, \"o => o.Firstname\".", ex);
            }
        }