这可能不会增加很多,但万一任何人都像我一样疯狂,这里是完整的代码,如果您需要使用DatePart/DateName为Dr. Zim的答案构建表达式树,还包括时间部分。显然,为了其他目的,您可以更改Product-> YourInitialType,ProductVM-> YourResultType和modified-> YourProperty。
编辑(1/23/08):由此生成的SQL在6.0.2和6.1.3之间更改。最初,如果值为null,则生成的SQL将创建一个空结果。在这种情况下,我认为这是可取的,但我可以看到为什么在其他情况下它不会被期望(null +“一个字符串值”= null),并且可能导致输出不等于您希望的值。我将详细介绍列输出如何在下面进行更改,但蹭它是现在这将输出空值“//::”。我只是在我的调用代码中处理这个输出作为特例,并手动将其更改为null,但其他人可能想要解决添加更强大的结果以确保空值输出为空。还值得注意的是,新版本中的SQL语句非常长。
public class UserProductVM {
...
private DateTime _modified;
public DateTime SetModified { set { _dateEvent = value; } }
public string Modified { get { return _modified.ToString("dd MMM yyyy @ HH:mm:ss"); } }
...
}
然后你将值赋给SetModified之,改变你这样的代码:
ParameterExpression paramExp = Expression.Parameter(typeof(Product));
string propertyName = "modified";
Expression propertyOrField = Expression.PropertyOrField(paramExp, propertyName);
MethodInfo datePartMethod = typeof(System.Data.Entity.SqlServer.SqlFunctions).GetMethods().Where(x => x.Name == "DatePart" && x.GetParameters().Length == 2 && x.GetParameters()[1].ParameterType == typeof(DateTime?)).First();
MethodInfo dateNameMethod = typeof(System.Data.Entity.SqlServer.SqlFunctions).GetMethods().Where(x => x.Name == "DateName" && x.GetParameters().Length == 2 && x.GetParameters()[1].ParameterType == typeof(DateTime?)).First();
MethodInfo stringConvertMethod = typeof(System.Data.Entity.SqlServer.SqlFunctions).GetMethods().Where(x => x.Name == "StringConvert" && x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == typeof(decimal?)).First();
MethodInfo stringConcatMethod = typeof(string).GetMethods().Where(x => x.Name == "Concat" && x.GetParameters().Length == 2 && x.GetParameters()[0].ParameterType == typeof(string) && x.GetParameters()[1].ParameterType == typeof(string)).First();
MethodInfo stringTrimMethod = typeof(string).GetMethods().Where(x => x.Name == "Trim" && x.GetParameters().Length == 0).First();
Type projectedType = typeof(ProductVM);
NewExpression newHolder = Expression.New(projectedType);
MemberInfo member = anonType.GetMember("modified")[0];
var monthPartExpression = Expression.Call(null, datePartMethod, Expression.Constant("mm", typeof(string)), propertyOrField);
var convertedMonthPartExpression = Expression.Call(null, stringConvertMethod, Expression.Convert(monthPartExpression, typeof(decimal?)));
var convertedDayPartExpression = Expression.Call(null, dateNameMethod, Expression.Constant("dd", typeof(string)), propertyOrField);
var convertedYearPartExpression = Expression.Call(null, dateNameMethod, Expression.Constant("yyyy", typeof(string)), propertyOrField);
var convertedHourPartExpression = Expression.Call(null, dateNameMethod, Expression.Constant("hh", typeof(string)), propertyOrField);
var convertedMinutePartExpression = Expression.Call(null, dateNameMethod, Expression.Constant("n", typeof(string)), propertyOrField);
var convertedSecondPartExpression = Expression.Call(null, dateNameMethod, Expression.Constant("ss", typeof(string)), propertyOrField);
var allAddedExpression = Expression.Call(null, stringConcatMethod,
convertedMonthPartExpression,
Expression.Call(null, stringConcatMethod,
Expression.Constant("/", typeof(string)),
Expression.Call(null, stringConcatMethod,
convertedDayPartExpression,
Expression.Call(null, stringConcatMethod,
Expression.Constant("/", typeof(string)),
Expression.Call(null, stringConcatMethod,
convertedYearPartExpression,
Expression.Call(null, stringConcatMethod,
Expression.Constant(" ", typeof(string)),
Expression.Call(null, stringConcatMethod,
convertedHourPartExpression,
Expression.Call(null, stringConcatMethod,
Expression.Constant(":", typeof(string)),
Expression.Call(null, stringConcatMethod,
convertedMinutePartExpression,
Expression.Call(null, stringConcatMethod,
Expression.Constant(":", typeof(string)),
convertedSecondPartExpression
))))))))));
var trimmedExpression = Expression.Call(allAddedExpression, stringTrimMethod, new Expression[] { });
var month = Expression.Bind(member, trimmedExpression);
MemberInitExpression memberInitExpression =
Expression.MemberInit(
newHolder,
new MemberBinding[] { month });
var lambda = Expression.Lambda<Func<Product, ProductVM>>(memberInitExpression, paramExp);
太棒了。我刚刚使用了我的POCO,然后在最后一分钟使用了.AsEnumerable()。选择(p ...并且它运行得非常好。 – 2011-03-21 02:18:16
“关于此主题的冗长文章”链接似乎已被打破。:-( – 2015-05-22 18:25:42
@BrokednGlass好的答案。谢谢 – User 2015-06-08 09:59:00