MalaAiko
MalaAiko

Reputation: 151

C# - How to get item from the list with complex type

I am new in c# and have a problem with lists.

I have a class Message:

public class Message
{
    public int MessageId { get; set; }
    public DateTime CreatedDate { get; set; }
    public string Text { get; set; }
    public string Autor { get; set; }
    public string Source { get; set; }
}

and class MessageHandler:

class MessageHandler
{
    private List<Message> _dummyMessages = new List<Message>()
       {
            new Message(){
                MessageId = 1,
                CreatedDate = new DateTime(2014, 5, 27),
                Text = "Srpska vodoprivreda...",
                Autor = "Marko Markovic",
                Source = "Twitter"

            },
            new Message(){
                MessageId = 2,
                CreatedDate = new DateTime(2014, 5, 27),
                Text = "Aerodrom Beograd...",
                Autor = "Zoran Zoric",
                Source = "B92"

            }
        };

    public List<Message> GetLatestMessages(int nrMessagesToReturn)
    {
        List<Message> retVal;

        retVal = this._dummyMessages.GetRange(0, nrMessagesToReturn);

        return retVal;
    }

   //todo: Search list _dummyMessages and get Source and
   //check is it equal to "Twitter"
}

My problem is that I dont know how to get Source from List<Message> :( I started like this:

public List<Message> SearchBySource()
{
    List<Message> retVal;

    foreach (Message m in _dummyMessages) 
    {
        //..........
    }
    return retVal;
}

but what to do to get Source from Message?

I want to take Source from Message and then check is it "Twitter", because I want to count all "Twitter" sources from my list...

Sorry for stupid question, and thanks for help!!

Upvotes: 4

Views: 4498

Answers (4)

user3652099
user3652099

Reputation: 41

class Program
{


     public static List<Message> SearchBySource()
        {
        MessageHandler msg = new MessageHandler();
        List<Message> msgContent = msg.GetLatestMessages(1);
        return msgContent;
        }

     static void Main(string[] args)
     {
      List<Message> mymess =  SearchBySource();
        foreach (Message m in mymess) 
        {
        Console.WriteLine(m.Source);
        }
         Console.ReadLine();
     } 

Example for the Code to get first entry , returns Twitter.

Upvotes: 1

Christos
Christos

Reputation: 53958

Try this one:

List<Message> retVal;

foreach (Message m in _dummyMessages) 
{
    // Check if the message's source is twitter
    if(Message.Source=="Twitter")
            retVal.Add(Message);
}

return retVal;

Or using LINQ:

return _dummyMessages.Where(x=>x.Source=="Twitter").ToList();

Both of the above code samples will go in the body of SearchBySource() method.

One suggestion I have to make is you use a parameter, like below:

public List<Message> SearchBySource(string source)
{
    return _dummyMessages.Where(x=>x.Source==source).ToList();
}

in order your method being more meaningfull. You are seraching by source, so you have to provide the source.

Upvotes: 3

Tim Schmelter
Tim Schmelter

Reputation: 460138

Source is a public property in your class, that means you can access it directly:

public List<Message> SearchBySource(string source)
{
    List<Message> retVal = new List<Message>();
    foreach (Message m in _dummyMessages) 
    {
        if(source == m.Source)
            retVal.Add(m);
    }
    return retVal;
}

You can also use List.Find with a predicate:

List<Message> retVal = _dummyMessages.Find(m => m.Source == source);

Another option is to use LINQ to count all messages with source="Twitter":

int countTwitter = _dummyMessages.Count(m => m.Source == "Twitter");

or to return a list with all messages that have source="Twitter":

List<Message> retVal = _dummyMessages.Where(m => m.Source == "Twitter").ToList();

Upvotes: 2

Mister Epic
Mister Epic

Reputation: 16733

 var count = _dummyMessages.Count(m => m.Source == "Twitter");

ref: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Upvotes: 4

Related Questions