在 C# 中的对象初始值设定项中分配数组值
Assign array value inside object initializer in c#
我有一个 class 如下。
public class PurgeRecord
{
public int Index { get; set; }
public string Source { get; set; }
public int PurgeFileID { get; set; }
public string AuthorisationID { get; set; }
public string RecordSystem { get; set; }
public string KeyName { get; set; }
public string[] KeyValues { get; set; }
public bool IsValid { get; set; }
public string Action { get; set; }
public string ErrorDetail { get; set; }
public string FileName { get; set; }
}
我得到一些由“|”分隔的字符串值进入字符串数组并按如下方式遍历它。
string[] test = Convert.ToString(values[5]).Split('|');
foreach (string key in test)
{
purgeRecord = new PurgeRecord()
{
KeyValues = key,
IsValid = true,
FileName = "XYZ"
};
lstPurgeRecords.Add(purgeRecord);
}
但是我在键上遇到错误,因为无法将字符串隐式转换为字符串[]。我尝试了很多方法,也尝试了谷歌搜索,但没有成功。
请帮忙。
Split
本身returnsString[]
Splits a string into substrings that are based on the characters in an array.
其语法
public string[] Split(params char[] separator)
只需更新您的代码
来自
string[] test = Convert.ToString(values[5]).Split('|');
和
string[] test = values[5]).Split('|');
您可以像这样简单地将它放在 string[]
中:
KeyValues = new string[] { key }
或更短的格式:
KeyValues = new [] { key }
KeyValues
属性 类型是 string
数组,当你试图初始化一个 PurgeRecord
实例时,你试图插入一个 string
到string[]
.
所以这是你应该做的:
//Every object instance in C# have the function 'ToString'.
string[] test = values[5].ToString().Split('|');
foreach (string key in test)
{
purgeRecord = new PurgeRecord()
{
//Create an array to insert in the values, but probably this is the KeyName
KeyValues = new[] { key },
IsValid = true,
FileName = "XYZ"
};
lstPurgeRecords.Add(purgeRecord);
}
Linq
也有一个很好的方法:
lstPurgeRecords = values[ 5 ]
.ToString()
.Split( '|' )
.Select( key => new PurgeRecord
{
KeyValues = new[] { key },
IsValid = true,
FileName = "XYZ"
} );
我有一个 class 如下。
public class PurgeRecord
{
public int Index { get; set; }
public string Source { get; set; }
public int PurgeFileID { get; set; }
public string AuthorisationID { get; set; }
public string RecordSystem { get; set; }
public string KeyName { get; set; }
public string[] KeyValues { get; set; }
public bool IsValid { get; set; }
public string Action { get; set; }
public string ErrorDetail { get; set; }
public string FileName { get; set; }
}
我得到一些由“|”分隔的字符串值进入字符串数组并按如下方式遍历它。
string[] test = Convert.ToString(values[5]).Split('|');
foreach (string key in test)
{
purgeRecord = new PurgeRecord()
{
KeyValues = key,
IsValid = true,
FileName = "XYZ"
};
lstPurgeRecords.Add(purgeRecord);
}
但是我在键上遇到错误,因为无法将字符串隐式转换为字符串[]。我尝试了很多方法,也尝试了谷歌搜索,但没有成功。
请帮忙。
Split
本身returnsString[]
Splits a string into substrings that are based on the characters in an array.
其语法
public string[] Split(params char[] separator)
只需更新您的代码
来自
string[] test = Convert.ToString(values[5]).Split('|');
和
string[] test = values[5]).Split('|');
您可以像这样简单地将它放在 string[]
中:
KeyValues = new string[] { key }
或更短的格式:
KeyValues = new [] { key }
KeyValues
属性 类型是 string
数组,当你试图初始化一个 PurgeRecord
实例时,你试图插入一个 string
到string[]
.
所以这是你应该做的:
//Every object instance in C# have the function 'ToString'.
string[] test = values[5].ToString().Split('|');
foreach (string key in test)
{
purgeRecord = new PurgeRecord()
{
//Create an array to insert in the values, but probably this is the KeyName
KeyValues = new[] { key },
IsValid = true,
FileName = "XYZ"
};
lstPurgeRecords.Add(purgeRecord);
}
Linq
也有一个很好的方法:
lstPurgeRecords = values[ 5 ]
.ToString()
.Split( '|' )
.Select( key => new PurgeRecord
{
KeyValues = new[] { key },
IsValid = true,
FileName = "XYZ"
} );