Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,
I have a dictionary collection where I stored name and value.
For instance i have 500 records stored in that collection just like below-
C#
Dictionary<string,int> dict=new Dictionary<string,int>();
dict.add("A",1);
dict.add("B",2);
--
dict.add("AAB",500);

I have a method name GetData(int PageNumber,Dictionary<string,int>() myDictionary)
it takes 2 aurguments.
First i will pass page number,and second i have collection.

I want to achieve, if i pass page number 1 then dictionary collection maintain only 1-100 key value pair.
If i passed 2 then dictionary collection maintain only 101-200 key value pair.
And keep going such fashion. means i want to create it dynamic approach
Any approach is greatly appreciated!


Below are my code which i have implemented, but it is static i want it dynamic-
C#
public Dictionary<string, int> CreateCollection()
        {
            dictionary.Add("A", 1);
            dictionary.Add("B", 2);
            dictionary.Add("C", 3);
            dictionary.Add("D", 4);
            dictionary.Add("E", 5);
            dictionary.Add("F", 6);
            dictionary.Add("G", 7);
            dictionary.Add("H", 8);
            dictionary.Add("I", 9);
            dictionary.Add("J", 10);

            return dictionary;
        }

        public void getdata(int pageNo,object myDictionary)
        {
            Dictionary<string, int> KeyValuePair = ((Dictionary<string, int>)myDictionary);
            if (pageNo == 1)// pageNo is the Datagrid Pager index in this case....
            {
                foreach (KeyValuePair<string, int> kvp in KeyValuePair.Take(5))
                {
                    Response.Write("Key " + kvp.Key);
                    Response.Write("Value "+kvp.Value);
                }
            }
        }

        protected void GetData_Click(object sender, EventArgs e)
        {
            getdata(1, this.CreateCollection());
        }
Posted
Updated 29-Jan-13 2:48am
v2

1 solution

Please change your code like below
C#
static void Main(string[] args)
       {

           var getData = GetData(2, CreateCollection());
       }
       public static Dictionary<string, int> CreateCollection()
       {
           var dictionary = new Dictionary<string, int>
                                {
                                    {"A", 1},
                                    {"B", 2},
                                    {"C", 3},
                                    {"D", 4},
                                    {"E", 5},
                                    {"F", 6},
                                    {"G", 7},
                                    {"H", 8},
                                    {"I", 9},
                                    {"J", 10}
                                };

           return dictionary;
       }
public static List<KeyValuePair<string, int>> GetData(int pageNo, Dictionary<string, int> myDictionary)
       {
           if (pageNo == 1)
           {
              return CreateCollection().Where(x => x.Value >= 1 && x.Value <= 5).ToList();
           }
           else if (pageNo == 2)
           {
               return CreateCollection().Where(x => x.Value >= 6 && x.Value <= 10).ToList();
           }
           return null;
       }
 
Share this answer
 
v3
Comments
Jameel VM 29-Jan-13 9:26am    
I have updated the answer
Jameel VM 4-Sep-13 3:03am    
so you should create a Custom class.Please create it as a separate question
shruthics 4-Sep-13 3:30am    
yes i created custom class
Jameel VM 4-Sep-13 5:05am    
then what's the pbm?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900