Solved

LINQ, alias name, vb.net

Posted on 2014-12-01
7
580 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 63

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 34

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 34

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

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 34

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 34

Author Comment

by:Mike Eghtebas
ID: 40474160
0
 
LVL 34

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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