1

I have a list of strings 'obAttachmentTypes' where each string looks like this: (ID|NAME|LEVEL). I'm trying to convert these strings into a list of AttachmentType objects so there are easier to manage. I use the .Select function to translate the list of strings, but Im having trouble trying to split the strings. Ive looked into using the Split() function, but I'm not sure how to use it in this case.

List<KeywordDataSetItem> obAttachmentTypes = GetAttachmentTypes(app);

    var attachTypes = obAttachmentTypes
                .Select(at => new AttachmentType
                {
                    //use split here?
                    AttachmentTypeId = at.AlphaNumericValue.Split(''),
                    AttachmentTypeName = at.AlphaNumericValue.Split(''),
                    IsPopular = true,
                    AttachmentTypeLevel = at.AlphaNumericValue.Split(''),
                })
                .ToList();
            return attachTypes;

1 Answer 1

2

You'll want to split the string around pipe symbols first, then grab strings from different indexes in the resulting array.

    var attachTypes = obAttachmentTypes
                .Select(at => at.AlphaNumericValue.Split('|'))
                .Select(arr => new AttachmentType
                {
                    AttachmentTypeId = arr[0],
                    AttachmentTypeName = arr[1],
                    IsPopular = true,
                    AttachmentTypeLevel = arr[2],
                })
                .ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.