0

I have an object like that

    public class MyModel
    {
        public int Key { get; set; }
        public string Value { get; set; } 
    }

And I have model like List<MyModel>

    model[0]=new MyModel(){Key = 1,Value ="Something"};
    model[1]=new MyModel(){Key = 3,Value ="Something else"};

I want this output :

    1. Value is = Something //there is \n here
    3. Value is = Something else

So the seperator is \n and while string.join(" \n ", ? ) what should I do?

Is that possible or not? I did it like that but I want to learn can I string.join() do that:

    var newArray = model.Select(x => ((x.Key)+". Value is ="+x.Value));
    string.join(" \n ",newArray );

Sorry for my bad english...

5
  • Have you tried? Do you have any problems with result? Commented Jan 28, 2021 at 8:14
  • 1
    The method is called string.Join() not string.join(). And you should remove the space before "\n" in the separator. But other than that, your code seems to deliver your desired result. Commented Jan 28, 2021 at 8:19
  • 1
    newArray is poorly named: the result of Select is an IEnumerable, not an array Commented Jan 28, 2021 at 8:21
  • @Backs yes my solution is works fine but I wondering is the string.join can do that without pre custimization or linq Commented Jan 28, 2021 at 8:46
  • @CaiusJard tnx bro I will be atend next time Commented Jan 28, 2021 at 8:49

2 Answers 2

1
var modelList = new List<MyModel>(){
        new MyModel(){ Key = 1, Value = "Value 1"},
        new MyModel(){ Key = 3, Value = "Value 2 with Key3"}
    };
    
    var stringArray = modelList.Select(model=> $"{model.Key}. Value={model.Value}");
    var finalString = String.Join('\n', stringArray);
    Console.Write(finalString);

https://dotnetfiddle.net/buCe8P

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't it same as my solution ? just different syntax in select
@BMErEr so yes. I just answered, because you asked. I have 10+ years of exp in C# and we often use String.Join() method. So "I want to learn can I string.join() do that" yes, you can, and it is the right way! The better solution would be String.Join(Environment.NewLine, stringArray); and much more better is using of StringBuilder.
0

You did well. There are other ways but they are no more correct (in some cases at least).

Example with custom ToString method:

var all = string.Join("\n", model);
public class MyModel
{
    public int Key { get; set; }
    public string Value { get; set; }

    public override string ToString() => $"{Key}. Value Is: {Value}";
}

Or directly with project array:

var all = string.Concat(model.Select(x => $"{x.Key}. Value Is: {x.Value}\n"));

but there's an unnecessary line brake at the end.

2 Comments

Remove the \n from the ToString and do it via string.Join
adding the linebreak directly in the ToString() is a bad idea because, this way the consumer can't decide whether to have a linebreak or not. Why not using string.Join() and use linebreak as separator?

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.