Hi I have a struct like this
typedef struct
{
string firstname;
string lastname;
} person;
As an input I take a List of strings List<string> like this
"firstname-lastname"
What is the fastest way to convert from the input string to the struct person? can it be done by using LINQ?
The string might not contain a ‘-‘ in that case the sting will be saved in person.firstname and the person.lastname is empty.
Hope I was clear.
Thanks a lot
I assume that you are coming from C++, because the naming and syntax is C++ and not C#. In C# it would be natural to use a class (you can also use a C# struct – just change the class keyword to struct):
With that class you can use linq to create a list:
The
letexpression makes the string split once and then reuses the result of the split.