private static string ExpressionTransform(this Expression expression) { StringBuilder result = new StringBuilder(); var candidates = new Queue <Operation>(new[] { new Operation { Expression = expression } }); while (candidates.Count > 0) { MemberExpression member = null; ConstantExpression constant = null; BinaryExpression binary = null; var ope = candidates.Dequeue(); // Console.WriteLine(ope.Expression); if (ope.Expression is BinaryExpression) { binary = ope.Expression as BinaryExpression; if (binary.Left is MemberExpression && binary.Right is ConstantExpression) { // like n => n.PartitionKey == "42" member = binary.Left as MemberExpression; constant = binary.Right as ConstantExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(), constant.FormatConstantExpression()).TrimStart()); } else if (binary.Left is ConstantExpression && binary.Right is MemberExpression) { // like n => "42" == n.PartitionKey member = binary.Right as MemberExpression; constant = binary.Left as ConstantExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(true), constant.FormatConstantExpression()).TrimStart()); } else if (binary.Left is BinaryExpression && binary.Right is BinaryExpression) { // like n => n.PartitionKey == "42" && n.RowKey == 42 candidates.Enqueue(new Operation() { Expression = binary.Right, TableOperand = binary.NodeType }); candidates.Enqueue(new Operation() { Expression = binary.Left }); } else if (binary.Left is MemberExpression && binary.Right is MemberExpression) { try { // like n => n.Date = DateTime.Now member = binary.Left as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(), binary.Right.EvaluateExpression()).TrimStart()); } catch { // like n => DateTime.Now = n.Date member = binary.Right as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(true), binary.Left.EvaluateExpression()).TrimStart()); } } else if (binary.Left is MemberExpression && binary.Right is NewExpression) { // like n => n.Date = new DateTime(2012, 12, 21); member = binary.Left as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(), binary.Right.EvaluateExpression()).TrimStart()); } else if (binary.Left is NewExpression && binary.Right is MemberExpression) { // like n => new DateTime(2012, 12, 21) == n.Date; member = binary.Right as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(true), binary.Left.EvaluateExpression()).TrimStart()); } else if (binary.Left is MemberExpression && binary.Right is MethodCallExpression) { // like n => n.UniqueIdentifier == Guid.Parse("52317684-641D-40C0-86C7-9B57DF97AC7F") member = binary.Left as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(), binary.Right.EvaluateExpression()).TrimStart()); } else if (binary.Left is MethodCallExpression && binary.Right is MemberExpression) { // like n => n.UniqueIdentifier == Guid.Parse("52317684-641D-40C0-86C7-9B57DF97AC7F") member = binary.Right as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(true), binary.Left.EvaluateExpression()).TrimStart()); } } else if (ope.Expression is MemberExpression) { // like n => n.IsEnabled member = ope.Expression as MemberExpression; result.Insert(0, string.Format("{0} ({1} eq true) ", ope.TableOperand.GetTableOperand(), member.Member.Name).TrimStart()); } else if (ope.Expression is UnaryExpression) { var unary = ope.Expression as UnaryExpression; // like n => !n.IsEnabled if (unary.Operand is MemberExpression) { member = unary.Operand as MemberExpression; result.Insert(0, string.Format("{0} ({1} eq false) ", ope.TableOperand.GetTableOperand(), member.Member.Name).TrimStart()); } } } return(result.ToString().Trim()); }