如何从列表中仅获取特定字段

发布于 2024-12-22 10:09:01 字数 282 浏览 1 评论 0原文

我有一个 IEnumerable 的 Lesson 对象:

IEnumerable<Lesson> filteredLessons

我通过以下方法将其转换为 List:

ToList();

但我希望返回的列表仅包含第一个属性 lessonid,而不是所有 Lesson代码>属性。

如何获取列表的特定属性的数据而不是对象的数据?

I have an IEnumerable of Lesson objects:

IEnumerable<Lesson> filteredLessons

I convert it to a List through the following method:

ToList();

But I want the returned list to contain only the first property, lessonid, not all the Lesson properties.

How can I get the data of specific property of the list instead of the objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

陈年往事 2024-12-29 10:09:01

您可以先选择所需的值,如下所示:

filteredLessons.Select(l => l.lessonId).ToList();

然后您将获得 ID 列表

You can select the value you want first, like this:

filteredLessons.Select(l => l.lessonId).ToList();

And you'll get a list of ID's

你的心境我的脸 2024-12-29 10:09:01

如果您想使用 linq 从列表中获取特定行值,请使用以下代码:

var name = from r in objClientList
           where r.ClientCode == Convert.ToInt32(drpClientsInternal.Items[i].Value)
           select r.IsInternalClient;

foreach (bool c in name)
{
    if (c)
    {
        ClientNameInternal = ClientNameInternal + drpClientsInternal.Items[i].Text +", ";
        drpClientsInternal.Items[i].Selected = true;
    }
}

If you want to get the the specific row value from list using linq use the following code:

var name = from r in objClientList
           where r.ClientCode == Convert.ToInt32(drpClientsInternal.Items[i].Value)
           select r.IsInternalClient;

foreach (bool c in name)
{
    if (c)
    {
        ClientNameInternal = ClientNameInternal + drpClientsInternal.Items[i].Text +", ";
        drpClientsInternal.Items[i].Selected = true;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文