【发布时间】:2017-12-01 05:34:08
【问题描述】:
以下是所需函数的外观:
public static Expression<Func<T, T>> GetExpression<T>(string propertyNames) where T : class
{
var properties = propertyNames.Split(
new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries
).ToList();
//need help here
}
目前我正在这样做:
_context.Questions.Select(q =>
new Question() {
QuestionId = q.QuestionId,
QuestionEnglish = q.QuestionEnglish
}
).ToList();
我想将其替换为:
_context.Questions.Select(GetExpression<Question>("QuestionId, QuestionInEnglish")).ToList();
任何帮助将不胜感激。
【问题讨论】:
-
我认为这不会是一个改进。看看 AutoMapper 什么的。
-
显式字符串属性名称是重构灾难的秘诀。如果您厌倦了每次手动编写投影,请将投影存储在某处或按照 AluanHaddad 的建议使用 automapper。但是,也许您应该禁用代理创建和查询
_context.Questions.AsNoTracking()...而不是将结果投影到它自己的类型?
标签: entity-framework linq expression