Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only specific field from the list

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?

like image 582
Anyname Donotcare Avatar asked Dec 21 '11 09:12

Anyname Donotcare


2 Answers

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

like image 71
Joakim Johansson Avatar answered Sep 19 '22 11:09

Joakim Johansson


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;
    }
}
like image 32
Mukul Avatar answered Sep 21 '22 11:09

Mukul