Solved

LINQ, alias name, vb.net

Posted on 2014-12-01
7
523 Views
Last Modified: 2016-02-15
The following code populates cmbSelectCustomer with CustomerID and works fine. I want display CustName by incorporating something like this with the LINQ:

Let CustName = c.FirstName & " " & c.LastName

And also have in code:

cmbSelectCustomer.DisplayMember = "CustName"
cmbSelectCustomer.ValueMember = "CustomerID"
 


Question: How can I revise this code to do above items?
       
 ' LOAD THE DATA TABLE
        Dim Cs = From c In db.tblCustomers _
                 Order By c.CustomerID _
                 Select c.CustomerID

        cmbSelectCustomer.Items.Clear()

        For Each cust In Cs
            cmbSelectCustomer.Items.Add(cust.ToString)
        Next

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 5
  • 2
7 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40473973
Hi eghtebas;

You can modify your code like the following to get what you need.

Dim Cs = (From c In db.tblCustomers _
          Order By c.CustomerID _
          Select CustName = c.FirstName & " " & c.LastName, c.CustomerID).ToArray()

cmbSelectCustomer.DataSource = Nothing
cmbSelectCustomer.DataSource = Cs

'' If DisplayMember and ValueMember are incorrect just reverse them.
cmbSelectCustomer.DisplayMember = "CustomerID"
cmbSelectCustomer.ValueMember = "CustName"

Open in new window

0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474020
Hi Fernando,

Only the following code is able to display some data as shown on the attache d image:
       Dim Cs = From c In db.tblCustomers _
         Order By c.CustomerID _
         Select c.CustomerID, CustName = c.FirstName & " " & c.LastName

        ' cmbSelectCustomer.DataSource = Nothing
        ' cmbSelectCustomer.DataSource = Cs

        For Each cust In Cs
            cmbSelectCustomer.Items.Add(cust.ToString)
        Next

Open in new window

cmbBox1.png
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474058
Maybe this is step in right direction but still no solution with for each or datasource options:
 Dim Cs As List(Of tblCustomer) = From c In db.tblCustomers _
         Order By c.CustomerID _
         Select c.CustomerID, CustName = c.FirstName & " " & c.LastName

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40474144
You have not implemented the query as I have posted it, use the ToArrat method on it. Also don't use  the For Each loop to fill the combo box use the DataSource method  and then use the DisplayMember and ValueMember.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474155
Wow, I totally goofed on that.

On another question I am preparing to post is I want to add Union Select 0, "- all -" as first record. I never done union with LINQ before.

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474160
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474501
If you intend to work on the new question, you may find this link below helpful:
http://stackoverflow.com/questions/4751924/linq-union-how-to-add-a-literal-value-to-the-query
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question