C
C#17mo ago
SWEETPONY

✅ how to avoid string.Join with empty strings?

I have List<IdentifierDto> Identifiers and I'm trying to get Title from Identifiers my code:
Identifier: subject.Identifiers?
.Select( identifier => identifier?.Title )
.JoinToString( "," )
Identifier: subject.Identifiers?
.Select( identifier => identifier?.Title )
.JoinToString( "," )
some times I get result: "," and I don't understand how can I fix this
7 Replies
SWEETPONY
SWEETPONY17mo ago
JoinToString is just string.Join
MODiX
MODiX17mo ago
Ero#1111
REPL Result: Success
string[] s = { };

string.Join(",", s)
string[] s = { };

string.Join(",", s)
Result: string




Compile: 534.603ms | Execution: 36.130ms | React with ❌ to remove this embed.
MODiX
MODiX17mo ago
Ero#1111
REPL Result: Success
string[] s = { "foo" };

string.Join(",", s)
string[] s = { "foo" };

string.Join(",", s)
Result: string
foo
foo
Compile: 542.459ms | Execution: 44.791ms | React with ❌ to remove this embed.
ero
ero17mo ago
Has to be 2 empty or null strings then Perhaps because identifier is null
SWEETPONY
SWEETPONY17mo ago
title is "" as I see using debugger
ero
ero17mo ago
Just put a .Where(t => !string.IsNullOrEmpty(t)) in between there
SWEETPONY
SWEETPONY17mo ago
ah.. I hadn't thought of that at all.. thanks