Julio Motol Julio Motol - 12 days ago 8
C# Question

Concatination of 2 Data inside a combobox

how to concatenate 2 data inside a combobox? here's my code:

private void loadPage(object sender, RoutedEventArgs e) {
using (var ctx = new Service()) {
try {
var query1 = from prod in ctx.products select prod.brand;
comboBox1.ItemsSource = query1.ToList();
var query4 = from hosp in ctx.hospitals select hosp.name;
comboBox4.ItemsSource = query4.ToList();
var query5 = (from cont in ctx.contactPersons select new { cont.lastName, cont.firstName }).First();
var fName = (query5.lastName + ", " + query5.firstName);
comboBox5.ItemsSource = fName.ToList();
} catch {
MessageBox.Show("ERROR 404: Database Not Found");
}
}
}


my mockup data is test(lastname) and test(firstname). it kinda funny since it outputs this:


t

e

s

t

,

t

e

s

t

Answer

ItemSource expect an IEnumerable, so you need to pass it an enumerable. but when you have a string and you ToList() it, you turn it to a list of char. so, you need to create an enumerable and add the the string you want. for example:

comboBox5.ItemsSource = new List<String>() {fName}

or, in your code example, just drop the "First()":

var query5 = ctx.contactPersons.Select(p => p.LastName + ", " + p.FirstName).ToList();

and you'll end up with an enumerable which you can set as an ItemSource for the combo box