?
Solved

LINQ, alias name, vb.net

Posted on 2014-12-01
7
Medium Priority
?
653 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 2000 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
Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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