How to get only specific field from the list

☆樱花仙子☆ 提交于 2021-02-08 13:34:06

问题


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?


回答1:


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




回答2:


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;
    }
}


来源:https://stackoverflow.com/questions/8587872/how-to-get-only-specific-field-from-the-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!