c# - 使用带有匿名类型 New Expression 的 DbFunctions 方法
问题描述
我需要构建一个表达式:
{
t => new {
field1 = t.field1,
field2 = t.field2,
dateTimeField = TruncateTime(t.dateTimeField)
}
}
我已经建立了表达:
{
t => new {
field1 = t.field1,
field2 = t.field2,
dateTimeField = t.dateTimeField
}
}
通过使用LatticeUtilsAnonymousTypes,但我不能t.dateTimeField
用TruncateTime
函数包装。
这是我的代码:
public static Expression<Func<T, dynamic>> GroupByExpressionAnonymous(string fields,
string groupByDateField = null)
{
ISet<string> propertyNames = new HashSet<string>(fields.Split(',').Select(t => t.Trim()).ToList());
if (!String.IsNullOrEmpty(groupByDateField))
{
propertyNames.Add(groupByDateField);
}
ParameterExpression entityParameterExpression = Expression.Parameter(typeof(T), @"t");
var properties = typeof(T).GetProperties().Where(p => propertyNames.Contains(p.Name));
var propertyExpressions = properties.Select(p => Expression.Property(entityParameterExpression, p));
var anonymousType = AnonymousTypeUtils.CreateType(properties.ToDictionary(p => p.Name, p => p.PropertyType));
var anonymousTypeConstructor = anonymousType.GetConstructors().Single();
var anonymousTypeMembers = anonymousType.GetProperties().Cast<MemberInfo>().ToArray();
var anonymousTypeNewExpression = Expression.New(anonymousTypeConstructor, propertyExpressions, anonymousTypeMembers);
return Expression.Lambda<Func<T, dynamic>>(anonymousTypeNewExpression, entityParameterExpression);
}
在这种情况下,如何将 DbFunction Truncate 用于表达式字段之一?
解决方案
推荐阅读
- c# - WPF DataGrid IsSelected 前景颜色动态变化
- android - Compose 中是否有等效的 LinearSnapHelper?
- advanced-custom-fields - 在 ACF 中显示嵌套在转发器字段组中的字段
- javascript - getStaticProps 和 getServerSideProps 中的 fetch 是否与本机浏览器 fetch API 相同?
- python - Anaconda 无法启动 Qt 控制台 ImportError: DLL load failed while importing win32api: %1 is not an valid Win32 application
- css - CSS 3D 矩形棱镜
- javascript - 在创建用户时将数据写入 Firestore 不起作用
- sas - 如何在 sas 的特定列中计算变量的频率
- api - 如何在 fetch() 期间减小图像大小
- javascript - 需要 node-pty 导致 TypeError: Object.setPrototypeOf: expected an object or null, got undefined