如何使用linq将对象扩展为基本属性 [英] How to expand object to primitive properties using linq
问题描述
假设我有以下课程:
public class StudentTeacher
{
public Student Student {get; set;}
public Teacher Teacher {get; set;}
public int OverlappingClasses {get; set;}
}
现在,在我的代码中,我有一个对象列表.
Now, in my code I have a list of objects.
List<StudentTeacher> studentTeacher = GetStudentTeachers();
现在,我需要选择所有属性作为属性而不是对象.
Now, I need to select all properties as properties not as objects.
因此,例如,我可以这样做:studentTeacher.Select(x=>x.Student)
它将返回对象列表(IEnumerable).
So, for example, I can do this: studentTeacher.Select(x=>x.Student)
and it will return a list of objects (IEnumerable).
但是当我选择多个属性时,即studentTeacher.Select(x => new {x.Student,x.Teacher}).ToList(),返回类型为IEnumerable {Anonymous {Student, Teacher}}
But when I select multiple properties, i.e. studentTeacher.Select(x=> new {x.Student, x.Teacher}).ToList() the return type is IEnumerable {Anonymous {Student, Teacher}}
如何,我可以使用仅具有属性的简单匿名类型来代替这种复杂的匿名类型. (因此,它将为Anonymous{StudentID, StudentName, StudentLastName, TeacherID, TeahcerName, TeacherLastName, etc.}
)
How, instead of this complex anonymous type I can have a simple anonymous type with properties only. (So, it will be Anonymous{StudentID, StudentName, StudentLastName, TeacherID, TeahcerName, TeacherLastName, etc.}
)
一种方法是通过在select子句中显式指定所有属性,但是在我的特殊用例中,我在每个对象中拥有> 30个属性,而属性的总数为> 100.)
One way to do this is by explicitly specifying all properties in the select clause but in my particular use case I have >30 properties in each object and the total # of properties is >100.)
推荐答案
我认为这是您要尝试做的事情:
I think this is what you're trying to do:
public class Student
{
public int StudentId
{
get;
set;
} = 1;
public string StudentName
{
get;
set;
} = "Name";
}
public class Teacher
{
public int TeacherId
{
get;
set;
} = 666;
public string TeacherName
{
get;
set;
} = "TeacherName";
}
public class StudentTeacher
{
public Student Student
{
get;
} = new Student();
public Teacher Teacher
{
get;
} = new Teacher();
}
public static dynamic GetAnonType(Student student, Teacher teacher)
{
var propertyNamesAndPropertiesStudent = student.GetType().GetProperties().Select(item => Tuple.Create($"{nameof(Student)}{item.Name}", item.GetMethod.Invoke(student, null)));
var propertyNamesAndPropertiesTeacher = teacher.GetType().GetProperties().Select(item => Tuple.Create($"{nameof(Teacher)}{item.Name}", item.GetMethod.Invoke(teacher, null)));
dynamic sampleObject = new ExpandoObject();
foreach(var propertyNamePropertyValuePair in propertyNamesAndPropertiesStudent)
{
((IDictionary<string, object>)sampleObject).Add(propertyNamePropertyValuePair.Item1, propertyNamePropertyValuePair.Item2);
}
foreach(var propertyNamePropertyValuePair in propertyNamesAndPropertiesTeacher)
{
((IDictionary<string, object>)sampleObject).Add(propertyNamePropertyValuePair.Item1, propertyNamePropertyValuePair.Item2);
}
return sampleObject;
}
您可以这样称呼:
var studentTeachers = new List<StudentTeacher> {new StudentTeacher()};
var anonTypes = studentTeachers.Select(item => GetAnonType(item.Student, item.Teacher));
anonTypes将是一个匿名类型的枚举,其成员为StudentId,StudentName,TeacherId,TeacherName.这应该缩放到更大的对象.
anonTypes will be an enumerable of anonymous types with members StudentId, StudentName, TeacherId, TeacherName. This should scale to larger objects.
这篇关于如何使用linq将对象扩展为基本属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!