如何迭代字符串列表并在 dbo 上调用它们?

发布于 2025-01-17 23:04:23 字数 526 浏览 0 评论 0原文

我是一个学生,很抱歉,这真的很愚蠢。我有一个名为“人”的模型。人们拥有大量属性,例如身高重量,眼颜料等。我可以使用

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紙鸢 2025-01-24 23:04:23

简单的答案是捕获属性本身(而不仅仅是其名称),然后使用 .GetValue() 方法从每个人处获取属性的值。

var properties = typeof(Person).GetProperties();
...
    array[j][i] = item.GetValue(person);

但乔尔在他的评论中提出了一个很好的观点。我会质疑您需要一个二维数组结构来保存每个人的所有值的假设。仔细看看你实际上想要解决的问题,因为可能有更好的方法来解决它。

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.

var properties = typeof(Person).GetProperties();
...
    array[j][i] = item.GetValue(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文