如何迭代字符串列表并在 dbo 上调用它们?
我是一个学生,很抱歉,这真的很愚蠢。我有一个名为“人”的模型。人们拥有大量属性,例如身高重量,眼颜料等。我可以使用
(typeof(person).getProperties()select t.name)
获得人的属性列表。然后,我需要将所有人员对象插入2D数组中,有一种方法与循环双重循环我可以使用列表访问DBO,而不是将每个属性称为硬码?
我在想像
foreach (var (item, i) in properties.Select((item, i ) => (item, i)))
{
array[0][i] = properties[i];
foreach (var(person, j) in people.Select((person, j)=>(person, j)))
{
//this is the part I'm struggling with
array[j][i] = People.property[i];//???
}
}
I'm a student so sorry if this is really stupid. I have a model called Person which is IEnumerable. Person holds a large amount of attributes such as height weight, eyecolor etc. I can get the properties of Person using
(from t in typeof(Person).GetProperties() select t.Name)
which results in a list. I need to then insert all the Person objects into a 2d array is there a way with a double for loop I can use the list to access the dbo instead of calling every property as hard code?
I was thinking something like
foreach (var (item, i) in properties.Select((item, i ) => (item, i)))
{
array[0][i] = properties[i];
foreach (var(person, j) in people.Select((person, j)=>(person, j)))
{
//this is the part I'm struggling with
array[j][i] = People.property[i];//???
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的答案是捕获属性本身(而不仅仅是其名称),然后使用
.GetValue()
方法从每个人处获取属性的值。但乔尔在他的评论中提出了一个很好的观点。我会质疑您需要一个二维数组结构来保存每个人的所有值的假设。仔细看看你实际上想要解决的问题,因为可能有更好的方法来解决它。
The simple answer is to capture the property itself (not just its name), and then use the
.GetValue()
method to get the property's value from each person.But Joel makes a good point in his comment. I would challenge the assumption that you need a 2d array structure to hold all the values for each Person. Take a good look at the problem you're actually trying to solve, because there's probably a better way to solve it.