Solved

LINQ, alias name, vb.net

Posted on 2014-12-01
7
473 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now