如何检索退回的物品数量?

时间:2014-06-08 16:59:54

标签: c#

我有以下代码:

Tweet.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assign3_Twitter
{
    public class Tweet
    {
        public string HashTag { get; private set; }
        public string Message { get; private set; }
        public string Sender { get; private set; }
        public string Recipient { get; private set; }
        public Tweet (string sender, string message, string hashtag, string reciepient)
        {
            this.Sender = sender;
            this.HashTag = hashtag;
            this.Message = message;
            this.Recipient = reciepient;
        }

        public override string ToString ()
        {
            return string.Format ("==========\nTweet\n==========\nFrom: {2} To: {3} Message: {1} Hashtag: {0}", HashTag, Message, Sender, Recipient);
        }
    }
}

TweetManager.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assign3_Twitter
{
    public class TweetManager
    {
        private List<Tweet> tweets = new List<Tweet>();

         public TweetManager()
        {
            Tweet tw1 = new Tweet ("Austen", "Hello World!", "#Hey", "Twitter");
            tweets.Add (tw1);
            Tweet tw2 = new Tweet ("Test1", "Hello World! x2", "#Howdy", "Tweeter");
            tweets.Add (tw2);
            Tweet tw3 = new Tweet ("Test2", "Hello World! x3", "#Hey", "Twitter");
            tweets.Add (tw3);
            Tweet tw4 = new Tweet ("Test3", "Hello World! x4", "#Howdy", "Tweeter");
            tweets.Add (tw4);
            Tweet tw5 = new Tweet ("Test4", "Hey there!", "#Hey", "Twitter");
            tweets.Add (tw5);
            Tweet tw6 = new Tweet ("Test5", "Woah this is cool!", "#Howdy", "Tweeter");
            tweets.Add (tw6);
            Tweet tw7 = new Tweet("Test 6", "Neat!", "#Hey", "Austen");
            tweets.Add(tw7);

        }

        public void SeeAllTweeters()
        {

            Console.WriteLine("==================================\nViewing all tweets \n==================================\n");

            foreach (Tweet Tweets in tweets)
            {
                Console.WriteLine(Tweets);
            }
        }

        public void SeeHashTag(string hashtag)
        {
            Console.WriteLine("==================================\nViewing all tweets containing the hashtag: " + hashtag + "\n==================================\n");
            foreach (Tweet Tweets in tweets)
            {
                if (Tweets.HashTag == hashtag)
                {
                    Console.WriteLine(Tweets);
                }
            }
        }

我希望在SeeHashTag()方法

中实现与以下内容类似的功能
Console.WriteLine("==================================\nViewing x number of tweets containing the hashtag: " + hashtag + "\n==================================\n");

它计算返回的数量。我不太确定如何做到这一点!

非常感谢任何帮助,谢谢:)

4 个答案:

答案 0 :(得分:1)

public void SeeHashTag(string hashtag)
{
    int numHashes = 0;
    string tweets = String.Empty;
    foreach (Tweet Tweets in tweets)
    {
        if (Tweets.HashTag == hashtag)
        {
            tweets += hashtag + '\n';
            numHashes++;
        }
    }
    Console.WriteLine("==================================\nViewing " + numHashes + "tweet" + numHashes == 1? "":"s" + " containing the hashtag: " + hashtag + "\n==================================\n");
    Console.Write(tweets);
}

答案 1 :(得分:1)

您可以使用LINQ Count()使用主题标签快速查询所有推文。它会是这样的:

var tweetCount = tweets.Count(t => t.HashTag == hashtag);
Console.Writeline(String.Format("Viewing {0} number of tweets containing the hashtag {1}",
     tweetCount, hashtag));

答案 2 :(得分:1)

首先过滤您的列表:

List<Tweet> tweetsWithHashtag = tweets.Where( x => x.HashTag == hashtag).ToList();

然后使用tweetsWithHashtag.Count获取带有#标签的推文数量。此外,您还可以在for循环中使用tweetsWithHashtag

答案 3 :(得分:0)

很抱歉回复很晚......我的电力消失了。我已经写了答案,所以无论如何我都要发帖......

首先,您应该使用for循环而不是foreach循环。 for循环比foreach循环快。从Patrick Smacchia的Blog:

中复制此内容

The fact that for is more efficient than foreach results from the fact that foreach is using an enumerator object behind the scene.

所以我已使用foreach方法中的for循环替换SeeHashTag()循环。

public void SeeHashTag(string hashtag)
{
    Console.WriteLine("==================================\nViewing all tweets containing the hashtag: " + hashtag + "\n==================================\n");
    for (int x = 0; x < tweets.Count; x++)
    {
        if (tweets[x].HashTag == hashtag)
        {
            Console.WriteLine(tweets[x]);
        }
    }
    Console.WriteLine("Total Number of tweets:" + tweets.Count(tw => tw.HashTag == hashtag));
}