简体   繁体   中英

Access the field of an object in a Linked List - C#

I want to find all the objects in a Linked List which satisfy a certain requirement. Their internal state (one field) must assume a specific value.

Consider these three classes (I will give only essential information):

public class Notification
{
    public DateTime timeStamp { get; private set; }

    public uint sourceApp { get; private set; }

    public int [,,] icon { get; private set; }


}

public class PearStack
{
    public LinkedList<Notification> notifications { get; private set; }
   //...
}

public class NotificationDispatcher
{
    public PearStack[] pearStacks { get; private set; }

    public NotificationDispatcher(uint s, PearStack pearstack)
    {
       //...
    }
    //...
}

In the constructor "NotificationDispatcher" I have as input a "PearStack" object and an integer.

A PearStack object (pearstack) has a linked list of Notifications.

I want to select all the objects (Notifications) of the linked list contained in pearstack which have the field "sourceapp" equal to "s".

I thought about:

pearstack.notifications.Find(s)

But it is wrong.

Can anyone help me please?

I'm pretty sure you can do this with a simple LINQ query:

var notificationsForApp = pearstack.notifications.Where(x => x.sourceApp == s);

This will give you an IEnumerable<Notification> where enumerating will only give you the notifications matching the source app. To enumerate immediately and get a List<Notification> or Notification[] , simply append ToList() or ToArray() to the end of the statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM