Julio Motol
Julio Motol

Reputation: 823

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

Upvotes: 0

Views: 74

Answers (2)

barakcaf
barakcaf

Reputation: 1324

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

Upvotes: 2

Fer Garc&#237;a
Fer Garc&#237;a

Reputation: 970

What about this:

 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 { fName = cont.lastName + ", " + cont.firstName });
                    comboBox5.ItemsSource = query5.ToList();
                } catch {
                    MessageBox.Show("ERROR 404: Database Not Found");
                }
            }
        }

fName.ToList() basically is generating a list of individual characters in fName (which is a string in your instance).

Upvotes: 0

Related Questions