Click here to Skip to main content
15,943,061 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. How is it possible to get the value of a certain index in a List<>.
I use Join when I have 1D list/array. But i Cannot use it for multidimensional, here's what Im trying to do

C#
protected void lnkStep3(object sender, EventArgs e)
   {
       List<OrderFields> listOrderBy = OrderBy();
       string strOrderByFields = "";
       for (int i = 0; i <= listOrderBy.Count - 1; i++)
       {
           strOrderByFields += listOrderBy[i][0];
       }
     
      
   }

public List<OrderFields> OrderBy()
    {
        List<OrderFields> listOrderBy = new List<OrderFields>();
        for (int i = 0; i <= gvFilters.Rows.Count - 1; i++)
        {
            TextBox txtOrder = (TextBox)gvFilters.Rows[i].FindControl("txtOrder");
            DropDownList drpTableName = (DropDownList)gvFilters.Rows[i].FindControl("drpTableName");
            DropDownList drpFieldName = (DropDownList)gvFilters.Rows[i].FindControl("drpFieldName");
            string ordervalue = drpTableName.SelectedValue.ToString() + "." + drpFieldName.SelectedValue.ToString();
            listOrderBy.Add(new OrderFields(Convert.ToInt32(txtOrder.Text), ordervalue));
        }
        listOrderBy.Sort(delegate(OrderFields x, OrderFields y)
        {
            return x.OrderNo.CompareTo(y.OrderNo);
        });
        return listOrderBy;
    }


What I have tried:

I am trying to concatenate the values of index[1] of strOrderByFields List with comma
Posted
Comments
Sergey Alexandrovich Kryukov 15-Feb-16 22:34pm    
There is no such thing as "multidimensional list". What many call a "multidimensional array" does exist, but, more correctly, this is an "array of rank N". The value of the list is taken by index using the operator []. Your problem in unclear and probably does not even exist; it's just the problem of your understanding. You may have a chance to get help if you explain what do you want to achieve, ultimately.
—SA
bjay tiamsic 16-Feb-16 0:30am    
I got this class.

public class OrderFields
{
public int OrderNo { get; set; }
public string OrderField { get; set; }

public OrderFields(int num, string field)
{
OrderNo = num;
OrderField = field;
}
}

I added items to it using the codes in my main post and I want to get the values of OrderField
Sinisa Hajnal 16-Feb-16 2:52am    
Then you take the element of the List that contains OrderField (which is by the way an object and even if you list <orderfield> was an array it would be single dimension). See the solution below.
bjay tiamsic 16-Feb-16 0:32am    
What to you call this by the way? List with 2 values like a 2D Array?
Sergey Alexandrovich Kryukov 16-Feb-16 1:01am    
List with two values is not even like "2D Array", it's a list with 2 elements. I cannot understand where your confusion comes from, but it looks like a big confusion...
—SA

To access the list item you can use the index as you're doing above.

But to take the object value you have to cast the list object to OrderField and then access the property normally.
C#
((OrderField)listOrderBy[i]).OrderField


Since the list is strongly typed, you might not even need to cast it explicitly.

This is the equivalent of creating new element
C#
OrderField of = new OrderField(1, 'test');
Console.Write(of.OrderField); 

would print the word test.
 
Share this answer
 
v2
I'm not sure what you want to achieve, but it seems you want to sort an array based on another one.

As i mentioned in comment to the Bill's comment, you can use List<Tuple<int, string>>
For example:
C#
List<Tuple<int, string>> a = new List<Tuple<int, string>>(){
	Tuple.Create(1, "1"),
	Tuple.Create(2, "2"),
	Tuple.Create(3, "3"),
	Tuple.Create(4, "4"),
	Tuple.Create(14, "14"),
	Tuple.Create(45, "45"),
	Tuple.Create(6, "6"),
	Tuple.Create(16, "16"),
	Tuple.Create(66, "66"),
	Tuple.Create(100, "100"),
	};

var b = a.OrderBy(x=>x.Item1);
//returns ordered list based on first item (integer): 1, 2, 3 ... 100
var c = a.OrderBy(x=>x.Item2);
//returns ordered list based on secod item (string): 1, 100, 14, 16 ... 66


In case i was wrong, please clarify your question using "Improve question" widget (green link at the right-bottom corner of the question).
 
Share this answer
 
Hi All!! I appreciate you response and I am really grateful for helping people like me.

I was able to make a research and this is what I found which worked for me
I created a class

C#
public class OrderFields
    {
        public int OrderNo { get; set; }
        public string OrderField { get; set; }

        public OrderFields(int num, string field)
        {
            OrderNo = num;
            OrderField = field;
        }
    }

protected void lnkStep3(object sender, EventArgs e)
{
        var newOrderByFields = new List<OrderFields>();
        newOrderByFields = listOrderBy;
        for (int i = 0; i <= listOrderBy.Count - 1; i++)
        {
            strOrderByFields +=                 newOrderByFields[i].OrderField.ToString()+",";
        }

}

But I will take your suggestions in mind and will try it in my program. Thank you very much! Please continue helping those in need of knowledge. Mabuhay!
 
Share this answer
 
v3
Comments
Philippe Mori 16-Feb-16 20:42pm    
It does not seems much different than the question...
bjay tiamsic 16-Feb-16 20:51pm    
Oops sorry I posted the wrong code. Will update

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