none
Reading first lines of a Dictionary RRS feed

  • Question

  • Greetings,

    I'm trying to reach to the first 6 items (I guess) of a dictionary and store each one of them into a variable.

    private static Dictionary<string, CharacterTitleRating> _ratings = new Dictionary<string, CharacterTitleRating>();
    public string NumberOneInRatings;
    public string NumberTwoInRatings;
    public string NumberThreeInRatings;
    public int NumberOneInRatingsPoints;
    public int NumberTwoInRatingsPoints;
    public int NumberThreeInRatingsPoints;
    
    /// how the items being added to the list:
    _ratings.Add(chr.Name, new CharacterTitleRating
    {
    	CharacterName = chr.Name,
    	TotalPoints = chr.Asda2TitlePoints
    });
    
    /// main function
    var ratings = _ratings.Values.ToList().OrderByDescending(r => r.TotalPoints).ToList();
    //1
    NumberOneInRatings = ratings.//FirstItem
    NumberOneInRatingsPoints = ratings//SecondItem
    //2
    NumberTwoInRatings = ratings.//ThirdItem
    NumberTwoInRatingsPoints = ratings//FourthItem
    //3
    NumberThreeInRatings = ratings.//FifthItem
    NumberThreeInRatingsPoints = ratings//SixthItem
    
    
    internal class CharacterTitleRating
    {
    	public string CharacterName { get; set; }
    	public int TotalPoints { get; set; }
    }

    I am not sure how the dictionary should be outputted after the "OrderByDescending", cause It's there where I am trying to get them,

    Any help is appreciated.

    Wednesday, March 7, 2018 10:15 PM

Answers

  • Hi Zuher,

    Your Dictionary result will get sorted in descending order based on the values in "TotalPoints" variable. Please into below. I manually add the value for just demonstration:

               _ratings.Add("ABC", new CharacterTitleRating
                {
                    CharacterName = "ABC",
    	            TotalPoints = 100
                });
    
                _ratings.Add("DEF", new CharacterTitleRating
                {
                    CharacterName = "DEF",
                    TotalPoints = 200
                });
    
                _ratings.Add("J", new CharacterTitleRating
                {
                    CharacterName = "J",
                    TotalPoints = 600
                });
    
                _ratings.Add("K", new CharacterTitleRating
                {
                    CharacterName = "K",
                    TotalPoints = 300
                });

    Before Sorting:

    After Sorting:

    And for the question how you can out put first "N" number of values you try thr following using Take(<How many value>). If you want to look more on Take() please look here. How Take() works is that it takes top number values that you pass in parameter.

                var ratings = _ratings.Values.ToList().OrderByDescending(r => r.TotalPoints).ToList();
                foreach (var key in ratings.Take(2))
                {
                    Console.WriteLine("{0}: {1}", key.TotalPoints, key.CharacterName);
                }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Thursday, March 8, 2018 5:07 AM
    Moderator

All replies

  • You can use System.Linq namespace and get the first n elements using 

    yourDictionary.Take(n);



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (kokkisajee) or Facebook (sajeetharan) profile for Sajeetharan at Stack Overflow, Q&A for professional and enthusiast programmers

    Wednesday, March 7, 2018 10:21 PM
  • Hello Zuher,

    Skip and Take of linq is good choice for you. Try the below code.

    ratings.Skip(0).Take(1);//FirstItem ratings.Skip(1).Take(1);//SecondItem ratings.Skip(2).Take(1);//ThirdItem

    ...


    Or

    ratings.Skip(0).FirstOrDefault();
    ratings.Skip(1).FirstOrDefault();
    ratings.Skip(2).FirstOrDefault();
    
    ...
    

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 8, 2018 2:13 AM
    Moderator
  • Hi Zuher,

    Your Dictionary result will get sorted in descending order based on the values in "TotalPoints" variable. Please into below. I manually add the value for just demonstration:

               _ratings.Add("ABC", new CharacterTitleRating
                {
                    CharacterName = "ABC",
    	            TotalPoints = 100
                });
    
                _ratings.Add("DEF", new CharacterTitleRating
                {
                    CharacterName = "DEF",
                    TotalPoints = 200
                });
    
                _ratings.Add("J", new CharacterTitleRating
                {
                    CharacterName = "J",
                    TotalPoints = 600
                });
    
                _ratings.Add("K", new CharacterTitleRating
                {
                    CharacterName = "K",
                    TotalPoints = 300
                });

    Before Sorting:

    After Sorting:

    And for the question how you can out put first "N" number of values you try thr following using Take(<How many value>). If you want to look more on Take() please look here. How Take() works is that it takes top number values that you pass in parameter.

                var ratings = _ratings.Values.ToList().OrderByDescending(r => r.TotalPoints).ToList();
                foreach (var key in ratings.Take(2))
                {
                    Console.WriteLine("{0}: {1}", key.TotalPoints, key.CharacterName);
                }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Thursday, March 8, 2018 5:07 AM
    Moderator

  • And for the question how you can out put first "N" number of values you try thr following using Take(<How many value>). If you want to look more on Take() please look here. How Take() works is that it takes top number values that you pass in parameter.

                var ratings = _ratings.Values.ToList().OrderByDescending(r => r.TotalPoints).ToList();
                foreach (var key in ratings.Take(2))
                {
                    Console.WriteLine("{0}: {1}", key.TotalPoints, key.CharacterName);
                }
    This seems right for reading the first two lines,
    but reading the other 4 lines does not work:
    foreach (var key in ratings.Take(2)) //FirstPlace
    {
    _NumberOneInRatings = key.CharacterName;
    _NumberOneInRatingsPoints = key.TotalPoints;
    }
    foreach (var key in ratings.Skip(2).Take(2)) //SecondPlace
    {
    _NumberTwoInRatings = key.CharacterName;
    _NumberTwoInRatingsPoints = key.TotalPoints;
    }
    foreach (var key in ratings.Skip(4).Take(2)) //ThirdPlace
    {
    _NumberThreeInRatings = key.CharacterName;
    _NumberThreeInRatingsPoints = key.TotalPoints;
    }
    Friday, March 9, 2018 11:48 AM
  • Hi Zuher Laith,
    It definitely not only just for 4 n umbers but also any number of range. Please see the below code. We are here to guide you how you can solve the issue. We do not do all the work, you have to figure it out how you can implement the guidance into your solution to achieve the goal.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Dictionary
    {
        class Program
        {
            private static Dictionary<string, CharacterTitleRating> _ratings = new Dictionary<string, CharacterTitleRating>();
            public string NumberOneInRatings;
            public string NumberTwoInRatings;
            public string NumberThreeInRatings;
            public int NumberOneInRatingsPoints;
            public int NumberTwoInRatingsPoints;
            public int NumberThreeInRatingsPoints;
    
            static void Main(string[] args)
            {
    
                _ratings.Add("ABC", new CharacterTitleRating
                {
                    CharacterName = "ABC",
                    TotalPoints = 100
                });
    
                _ratings.Add("DEF", new CharacterTitleRating
                {
                    CharacterName = "DEF",
                    TotalPoints = 200
                });
    
                _ratings.Add("J", new CharacterTitleRating
                {
                    CharacterName = "J",
                    TotalPoints = 600
                });
    
                _ratings.Add("K", new CharacterTitleRating
                {
                    CharacterName = "K",
                    TotalPoints = 700
                });
    
                _ratings.Add("L", new CharacterTitleRating
                {
                    CharacterName = "L",
                    TotalPoints = 800
                });
    
                _ratings.Add("O", new CharacterTitleRating
              {
                  CharacterName = "O",
                  TotalPoints = 900
              });
    
                var ratings = _ratings.Values.ToList().OrderByDescending(r => r.TotalPoints).ToList();
                foreach (var key in ratings.Take(4))
                {
                    Console.WriteLine("{0}: {1}", key.TotalPoints, key.CharacterName);
                }
    
                Console.ReadLine();
    
    
            }
        }
    
        internal class CharacterTitleRating
        {
            public string CharacterName { get; set; }
            public int TotalPoints { get; set; }
        }
    }
    

    Output:


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, March 12, 2018 9:23 AM
    Moderator