简体   繁体   中英

Why is the delimiter excluded in this String.Join? C#

I have an ObservableCollection<T> that I use for binding that I want to put into a String.Join statement, but I don't understand why it is giving the results I am getting and how to fix it?

This is the code I am using to get the result,

First I am getting the data I need via this LINQ query,

public static IEnumerable<string> GetNursingHomeNames(string home)
{
    return DataContext.NursingHomeNameServerTables.Where(p => p.Nursing_Home_Section == home)
                        .Select(p => p.Nursing_Home_Name).Distinct();
}

I then put it into the ObservableCollection<T> (You may be wondering why I am not using an ObservableCollection<T> with the LINQ query, but I have my reasons.)

public static void NursingHomeNamesCollection(ObservableCollection<string> nursingHomeNames, string nursingHomeSection)
{
    var homeNames = GetNursingHomeNames(nursingHomeSection);
    if (homeNames == null)
    {
        return;
    }
    foreach (var item in homeNames)
    {
        nursingHomeNames.Add(item);
    }
}

This is the property in the main window,

 public ObservableCollection<string> NursingHomeNames { get; set; } =
                   new ObservableCollection<string>(); 

Then

Than I use Join to get the results for a specific purpose I need,

var result = String.Join(@",", NursingHomeNames.ToList());

And this gives the following result where there is no delimiter only a space,

foo bar bat baz

However, if just do this,

ObservableCollection<string> observableCol = new ObservableCollection<string>() { "foo", "bar", "bat", "baz" };

var result = String.Join(@",", observableCol.ToList());

The result is displayed with the delimiters in place.

foo,bar,bat,baz

Why is it doing this and is there a way to ensure the delimiters are correctly placed?

I know I have to work on my naming conventions.

EDIT: In the debuger, this is what I see,

When assigning the collection to a variable named data and viewing the results in the Watch Window

var data = NursingHomeNames.ToList();

Count = 4

[0] "foo"

[1] "bar"

[2] "bat"

[3] "baz"

However, I cannot reproduce this using any other code that does not use the LINQ query that pulls the data from the database. I tried making a new list and passing that list through the same code, but the error does not occur. I am sorry, but I can't post an example that can be reproduced.

As it turns out, after weeks of effort to figure this out, the answer to be had was with the comment that @Panagiotis Kanavos and @CodeCaster made.

I was using a Unicode character that looked like a comma and it was therefore creating a different behavior than what I was expecting.

In method

public static void NursingHomeNamesCollection(string nursingHomeSection)

you get string parameter to input. After in this method scope you add this string into static collection, but item in this scope is a char .

foreach (var item in homeNames)

You're trying to add a character at a time and join one big string. You need get collection to input of this method.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM