匿名类型的c# LINQ构建表达式

本文关键字:LINQ 构建 表达式 类型 | 更新日期: 2023-09-27 17:50:56

我有代码构建列表只有一个属性"Name"。如何修改代码,使其能够构建具有两个属性"Name"answers"Test_Result"的列表我知道匿名类型可以用于执行此操作,但是如何将它们转换为动态表达式呢?下面是我的代码:

string item = "Name";
string item2 = "Test_Result";
Type studentType = typeof(Student);
ParameterExpression itemParam = Expression.Parameter(studentType, item);
MemberInfo itemProperty = studentType.GetProperty(item);
MemberExpression valueInItemField = 
    Expression.MakeMemberAccess(itemParam, itemProperty);
Expression<Func<Student, string>> selectExpression =
    Expression<Func<Student, string>>
        .Lambda<Func<Student, string>>(valueInItemField, itemParam);
IEnumerable<string> currentItemFields = 
    DeserializedStudents.Select(selectExpression.Compile());

匿名类型的c# LINQ构建表达式

我假设这里的"Name"answers"Test_Result"是灵活的,不能硬编码。

匿名类型是完全定义的常规类;唯一有趣的是,编译器提供细节,而不是你。

我建议处理这种情况的方法是使用Tuple.Create创建IEnumerable<Tuple<string,string>>,并将它们称为Item1, Item2(来自Tuple<,>的名称)。另一种选择是使用ExpandoObject之类的东西,然后使用IDictionary<string,object> API或dynamic API来获取值。

例如:

string item1 = "Name";
string item2 = "Test_Result";
Type studentType = typeof(Student);
var itemParam = Expression.Parameter(studentType, "x");
var member1 = Expression.PropertyOrField(itemParam, item1);
var member2 = Expression.PropertyOrField(itemParam, item2);
var selector = Expression.Call(typeof(Tuple), "Create",
    new[] { member1.Type, member2.Type }, member1, member2);
var lambda = Expression.Lambda<Func<Student, Tuple<string,string>>>(
    selector, itemParam);
var currentItemFields = students.Select(lambda.Compile());

下面是相同的投影到成员nameresult的自定义类型:

class ProjectedData
{
    public string name { get; set; }
    public string result { get; set; }
}
...
string item1 = "Name";
string item2 = "Test_Result";
Type studentType = typeof(Student);
var itemParam = Expression.Parameter(studentType, "x");
var member1 = Expression.PropertyOrField(itemParam, item1);
var member2 = Expression.PropertyOrField(itemParam, item2);
var selector = Expression.MemberInit(Expression.New(typeof(ProjectedData)),
    Expression.Bind(typeof(ProjectedData).GetMember("name").Single(), member1),
    Expression.Bind(typeof(ProjectedData).GetMember("result").Single(), member2)
);
var lambda = Expression.Lambda<Func<Student, ProjectedData>>(
    selector, itemParam);
var currentItemFields = students.Select(lambda.Compile());

或者使用字典的方法:

string[] fields = {"Name", "Test_Result"};
Type studentType = typeof(Student);
var itemParam = Expression.Parameter(studentType, "x");
var addMethod = typeof(Dictionary<string, object>).GetMethod(
    "Add", new[] { typeof(string), typeof(object) });
var selector = Expression.ListInit(
        Expression.New(typeof(Dictionary<string,object>)),
        fields.Select(field => Expression.ElementInit(addMethod,
            Expression.Constant(field),
            Expression.Convert(
                Expression.PropertyOrField(itemParam, field),
                typeof(object)
            )
        )));
var lambda = Expression.Lambda<Func<Student, Dictionary<string,object>>>(
    selector, itemParam);
var currentItemFields = students.Select(lambda.Compile());