获取列表c的索引值# [英] Getting the value of index of list c#

查看:90
本文介绍了获取列表c的索引值#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。如何在List<>中获取某个索引的值。

当我有1D列表/数组时,我使用Join。但是我不能用它来做多维,这就是我想做的事情



  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 列表< 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(委托(OrderFields x,OrderFields y)
{
return x.OrderNo.CompareTo(y.OrderNo);
});
return listOrderBy;
}





我的尝试:



我试图用逗号连接strOrderByFields List的index [1]的值

解决方案

要访问列表项,你可以使用索引正如你上面所做的那样。



但是要获取对象值,你必须将列表对象强制转换为OrderField,然后正常访问该属性。

((OrderField)listOrderBy [i])。OrderField 





由于列表强烈键入,你可能甚至不需要明确地转换它。



这相当于创建新元素

 OrderField of =  new  OrderField( 1 '  test'); 
Console.Write(ofOrderField);



将打印单词test。


我不确定你想要实现什么,但似乎你想要排序一个基于另一个数组的数组。



正如我在对Bill的评论中提到的那样,你可以使用 List< Tuple< int,string> >

例如:

 List< Tuple< int,string>> a =  new 列表<元组< int,字符串>>(){
Tuple.Create( 1 1),
Tuple.Create( 2 2),
元组。创建( 3 3) ,
Tuple.Create( 4 4),
Tuple.Create( 14 14),
Tuple.Create( 45 45),
Tuple.Create( 6 6),
Tuple.Create( 16 16),
元组。创建( 66 66) ,
Tuple.Create( 100 100),
};

var b = a.OrderBy(x => x.Item1);
// 根据第一项(整数)返回有序列表:1,2,3 ... 100
var c = a.OrderBy(x => x.Item2);
// 返回基于secod项目(字符串)的有序列表:1,100,14,16 .. .66





如果我错了,请使用改善问题小部件澄清您的问题(绿色链接在问题的右下角。)


大家好!我很感谢你的回应,我非常感谢能帮助像我这样的人。



我能够进行研究,这就是我发现的对我有用的事情。 />
我创建了一个类



  public  < span class =code-keyword> class  OrderFields 
{
public int OrderNo { get ; set ; }
public string OrderField { get ; set ; }

public OrderFields( int num,字符串字段)
{
OrderNo = num;
OrderField = field;
}
}

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



}



但我会考虑你的建议并试用它我的节目。非常感谢你!请继续帮助那些需要知识的人。马布海!


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

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

解决方案

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.

((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

OrderField of = new OrderField(1, 'test');
Console.Write(of.OrderField); 


would print the word test.


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:

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).


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

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!


这篇关于获取列表c的索引值#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆