I have a string message like this: 8=ABCD 5=DCBA 88=2 39=D
And i have an entity:
public class Order : Entity {
public string Account {get;set;}
public char Type {get;set;}
public string Status {get;set;}
public string Something {get;set;}
}
And each attribute of the entity is equal to a number of the string pattern
8 = Account;
5 = Type;
88 = Status;
39 = Something;
I need to parse the string pattern to Order entity.
Anyone knows a solution to do this? without a loop or something like
Nothing inbuilt; you'd have to parse it yourself; for example:
using System;
using System.Linq;
using System.Text.RegularExpressions;
static class P
{
static void Main()
{
string input = "8=ABCD 5=DCBA 88=2 39=D";
var obj = new Order();
foreach (Match match in Regex.Matches(input, @"(?<=(\s|^))(?'key'[0-9]+)=(?'value'\S+)(?=($|\s))"))
{
var val = match.Groups["value"].Value;
switch (match.Groups["key"].Value)
{
case "8":
obj.Account = val;
break;
case "5":
obj.Type = val.FirstOrDefault(); // D from "DCBA" in the example
break;
case "88":
obj.Status = val;
break;
case "39":
obj.Something = val;
break;
}
}
Console.WriteLine(obj.Account); // ABCD
Console.WriteLine(obj.Type); // D
Console.WriteLine(obj.Status); // 2
Console.WriteLine(obj.Something); // D
}
}
public class Entity { }
public class Order : Entity
{
public string Account { get; set; }
public char Type { get; set; }
public string Status { get; set; }
public string Something { get; set; }
}
As for "without a loop": you are fundamentally picking out multiple tokens here; you could try to write ways of doing this without a loop, but it doesn't seem a sensible goal, frankly.