Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将字符串数组强制转换为C中的类型#_C#_Asp.net_Devexpress - Fatal编程技术网

C# 如何将字符串数组强制转换为C中的类型#

C# 如何将字符串数组强制转换为C中的类型#,c#,asp.net,devexpress,C#,Asp.net,Devexpress,我使用ASPxGridView PerformCallback方法将javascript值传递给后台代码,它可以工作。但我需要在类型中转换字符串数组,并绑定到ASPxGridView。我怎么做 protected void detailGrid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) { Group [] data = (Group)e.Parameters.Split(

我使用ASPxGridView PerformCallback方法将javascript值传递给后台代码,它可以工作。但我需要在类型中转换字符串数组,并绑定到ASPxGridView。我怎么做

  protected void detailGrid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        Group [] data = (Group)e.Parameters.Split(';');

        List<Group> l = new List<Group>();
        for (int i = 0; i < data.Length; i++)
        {
            l.Add(data[i]);
        }

        XFGridView1.DataSource = data;
        XFGridView1.DataBind();

    }
受保护的void detailGrid\u CustomCallback(对象发送方,ASPxGridViewCustomCallbackEventArgs e)
{
组[]数据=(组)e.Parameters.Split(“;”);
列表l=新列表();
for(int i=0;i
您可以使用

假设您的
类如下:

class Group
{
    public string MyProperty { get; set; }
}
然后你可以做:

string parameters = "abc,def,hij,klm,nop";
string[] myArray = parameters.Split(',');
Group[] groupArray  = Array.ConvertAll<string, Group>(myArray, delegate(string str)
             {
                 return  new Group { MyProperty = str };

             });
string parameters=“abc、def、hij、klm、nop”;
字符串[]myArray=parameters.Split(',');
Group[]groupArray=Array.ConvertAll(myArray,delegate(string str)
{
返回新组{MyProperty=str};
});
上面的代码将获取一个字符串
参数
在字符(
)上拆分它,然后使用array.ConvertAll将字符串数组转换为
数组。ConvertAll,
类有一个属性
MyProperty
,该属性将用字符串元素填充。

LINQ适用于“转换”或“选择”:

IEnumerable data=e.Parameters.Split(“;”).Select(p=>newgroup(p));
//或
IEnumerable data=e.Parameters.Split(“;”).Select(p=>newgroup{SomeProperty=p});

假设您的组类有一个构造函数,该构造函数分别接受字符串值或您希望填充的某些属性。

您到底需要什么?是否需要针对数组中的每个字符串元素创建一个组类对象?是的。这是我想要的。感谢您明确定义我需要的内容。+1对于LINQ方法,您可以使用
.ToArray()
最后,要获得OPI所需的组数组,请怀疑GridView实际上并不需要数组,但可能(希望如此)需要一个更通用的接口,如IEnumerable或IList。在这种情况下,也可以选择.ToList()。
IEnumerable<Group> data = e.Parameters.Split(';').Select(p=>new Group(p));  
//or
IEnumerable<Group> data = e.Parameters.Split(';').Select(p=>new Group{SomeProperty=p});