Solved

LINQ, alias name, vb.net

Posted on 2014-12-01
7
496 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

12 Experts available now in Live!

Get 1:1 Help Now