[英]C# insert values from string[] into list of model
I have this List of type model List<SequenceModel> listSequenceModel
which contains few properties, like我有这个 model List<SequenceModel> listSequenceModel
类型的列表,其中包含一些属性,例如
public class SequenceModel
{
public int Sequence1 {get;set;}
public int Sequence2 {get;set;}
public int Sequence3 {get;set;}
public int OrderNr {get;set;}
}
I am getting this string with "_" which I split and create a List of string我用“_”得到这个字符串,我拆分并创建了一个字符串列表
string[] szFilterParamsList2 = model.szFilterParams.Split('_');
An example of this string would be "sequence1a_sequence1b_sequence1c", but it can also be "_ sequence2b _", or "__sequence3" or also "sequence1 __" (spaces here are for stackoverflow format reason, ignore them).该字符串的一个示例是“sequence1a_sequence1b_sequence1c”,但也可以是“_ sequence2b _”或“__sequence3”或“sequence1 __”(此处的空格是出于stackoverflow格式的原因,请忽略它们)。 It means I can get a list with three non nullable elements, or any variation of it with nullables, but at least one element must have a value.这意味着我可以得到一个包含三个不可为空元素的列表,或者它的任何可空变体,但至少一个元素必须有一个值。
List1: [0] = "123"
[1] = "432"
[2] = "575"
List2: [0] = "123"
[1] = ""
[2] = ""
List3: [0] = ""
[1] = "98"
[2] = ""
and so on.等等。
How can I assign these values form the string[] into the List<SequenceModel> listSequenceModel
?如何将这些值从 string[] 分配到List<SequenceModel> listSequenceModel
? For the empty elements I will assign hardcoded -1, since the values should always be positive (if not null).对于空元素,我将分配硬编码 -1,因为值应该始终为正(如果不是 null)。
EDIT: SequenceModel is used in another model like编辑: SequenceModel 用于另一个 model 之类的
public class BarcodeModel
{
public int Product {get;set;}
public decimal Price {get;set;}
public List<SequenceModel> BarcodeSequence {get;set;}
}
Could this help you using Linq?这对您使用 Linq 有帮助吗?
class Program
{
static void Main(string[] args)
{
SplitStrings();
}
private static string sequence1 = "123_456_789";
private static string sequence2 = "123__";
private static string sequence3 = "__789";
static void SplitStrings()
{
string[] szFilterParamsList1 = sequence1.Split('_').Select(s => string.IsNullOrEmpty(s) ? "-1" : s).ToArray();
string[] szFilterParamsList2 = sequence2.Split('_').Select(s => string.IsNullOrEmpty(s) ? "-1" : s).ToArray();
string[] szFilterParamsList3 = sequence3.Split('_').Select(s => string.IsNullOrEmpty(s) ? "-1" : s).ToArray();
PrintArray(szFilterParamsList1);
PrintArray(szFilterParamsList2);
PrintArray(szFilterParamsList3);
}
static void PrintArray(string[] strArray)
{
foreach (string item in strArray)
{
Console.WriteLine(item);
}
}
}
Output: Output:
123
456
789
123
-1
-1
-1
-1
789
Assuming that the array will always have 3 items, I would do something like this:假设数组总是有 3 个项目,我会做这样的事情:
// 0. Make the string "-1" if it has no value
private string NormalizeSequenceValue(string s){
return string.IsNullOrEmpty(s)? "-1" : s;
}
// 1. Map the array to a SequenceModel
private SequenceModel ToSequenceModel(string [] array, int index){
if(array.Length != 3)
throw new ArgumentException("Array should always contain 3 elements")
return new SequenceModel{
Sequence1 = NormalizeSequenceValue(array[0]),
Sequence2 = NormalizeSequenceValue(array[1]),
Sequence3 = NormalizeSequenceValue(array[2]),
OrderNr = index
}
}
//2. Map a list of arrays to a list of SequenceModel
private List<SequenceModel> ToSequenceModels(List<string[]> rawSequences){
return rawSequences.Select((value, index) => ToSequenceModel(value, index)).ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.