I used string.Join in lambda expression to form comma seperated values
I achieved it using the following code:
var viewData = queue.Select(items => new companyQueueWithSegInfo()
{
segmentName = string.Join(",", items.Select(i => i.seginfo.Trim()));
}).AsQueryable()
}
The output for this will be :
AB ,CD
But I need output as
AB, CD
I tried like this:
string.Join(" ,",items.Select(i => i.segminfo)).Replace(",", ", ").Replace(" ,","")
Can anyone help me with this? but it didn't work.
If seginfo
is a string
, how about Trim
them first and then then join with ", "
?
string.Join(", ", items.Select(i => i.seginfo.Trim()));
Also you should check your item is null
or not for prevent NRE like;
string.Join(", ", list.Where(s => s != null).Select(i => i.Trim()))
or can use IsNullOrEmpty
as others mentioned.
You can use Trim()
for removing all leading and trailing white-space characters. And then just change ","
to ", "
:
var result = string.Join(", ", items.Where(x => !String.IsNullOrEmpty(x))
.Select(i => i.seginfo.Trim()));
Be sure to check if the string is empty or null also as I did.
Try this using Trim() ie, you need to join them with ", "
and then use the Trim():
string.Join(", ",items.Select(i => i.seginfo.Trim()))
To handle NULL condition of string try this:
string.Join(", ", items.Where(x => !String.IsNullOrEmpty(x))
.Select(i => i.seginfo.Trim()));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.