Solved

LINQ, union query, vb.net

Posted on 2014-12-01
19
526 Views
Last Modified: 2016-02-15
The following code works fine.

Question: How can I add Union Select 0, "- all -" to it?
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


You may find the following link helpful:
http://stackoverflow.com/questions/4751924/linq-union-how-to-add-a-literal-value-to-the-query
0
Comment
Question by:Mike Eghtebas
  • 9
  • 8
  • 2
19 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40474735
Hi eghtebas;

To add a string literal to the collection of the query results just add a new variable to the select as shown below.

In order to perform an Union operation the two list must be of the same data types. Not sure how you want to use the Union so if you can give more details.

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

Open in new window

0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474777
My existing code is:
        Dim Cs = (From c In db.tblCustomers _
          Order By c.CustomerID _
          Select ID = c.CustomerID, CustName = c.FirstName + " " + c.LastName).ToArray

        cmbSelectCustomer.DataSource = Nothing

        cmbSelectCustomer.DataSource = Cs

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

Open in new window


How would I incorporate this with what you have? I tried something similar but got no where.
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40474780
I don't know the exactly sintax in VB so i use C# (in bold) and you translate the sentences

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

Cs.Add( new {CustName = "--all--", CustomerID = 0} );

Best regards
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474785
Walter, I got an error:
Error      1      Type or 'With' expected.      

 Cs.Add( new {CustName = "--all--", CustomerID = 0} );
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40474787
Hi eghtebas;

What are you looking to do? Please explain in detail.

Thanks.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474793
Cs returns IDs and Names and populates a combo box.  Question: How can I have  ID=0 and Name="- all -" on the very top of the list?


ValueMember       DisplayMember
---------------            ---------------------
0                                    - all -                     <-- I want to add this top line
1                                    Name1
2                                    Name2
3                                    Name3
4                                    Name4
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40474814
I don't know the exactly sintax in VB so i use C# (in bold) and you translate the sentences
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474832
Walter,

I did translate it to vb using (http://converter.telerik.com/), I got:

Cs.Add(New With {Key .CustName = "- all -", Key .CustomerID = 0})

When I try this I get another error (and I do not know enough vb.net to correct this):
Error      1      Value of type '<anonymous type> (line 23)' cannot be converted to '<anonymous type> (line 19)'.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40474891
Hi eghtebas;

In the following code snippet I modified the query to return a concrete type to replace the Anonymous type it was returning in order to make it simpler to add the data you need.

Dim Cs As List(Of ReturnData) = (From c In db.tblCustomers _
                                 Order By c.CustomerID _
                                 Select New ReturnData With _
                                 { _
                                   CustName = c.FirstName & " " & c.LastName, _
                                   c.CustomerID
                                 }.ToList()

Dim newReturnData As New ReturnData() With {.CustName = "-all", .CustomerID = "0"}
Cs.Insert(0, newReturnData)

cmbSelectCustomer.DataSource = Nothing
cmbSelectCustomer.DataSource = Cs

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

'' Data being returned from the Linq query
Public Class ReturnData
    Public Property CustName As String
    Public Property CustomerID As Integer
End Class

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40474904
The reason why I changed the query to return a concrete type of ReturnData is because of this error message you are now getting.

Error      1      Value of type '<anonymous type> (line 23)' cannot be converted to '<anonymous type> (line 19)'.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40474935
I tried once but couldn't make it work. Most likely I didn't implemented it correctly. I need to come back to it later and update you on the outcome.

Thank you for the great help.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40475003
Hi Fernando,

There are two errors (one is repeat error on Cs). Please see this image for their locations and descriptions.3-Errors
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40475093
On the last line of the query you are missing the, ),  it should be as follows.

C.CustomerID } ).ToList()
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40475154
I added )
The same errors still show on the same spots.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40475163
Please repost the code. Thanks.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40475165
Sure:
     Private Sub LoadCustomers()
        Dim Cs As List(Of ReturnData) = (From c In db.tblCustomers _
                                 Order By c.CustomerID _
                                 Select New ReturnData With _
                                 {CustName = c.FirstName & " " & c.LastName, c.CustomerID } ).ToList()

        Dim newReturnData As New ReturnData() With {.CustName = "-all", .CustomerID = "0"}
        Cs.Insert(0, newReturnData)
        cmbCustomer.DataSource = Nothing
        cmbCustomer.DataSource = Cs
        cmbCustomer.DisplayMember = "CustomerID"
        cmbCustomer.ValueMember = "CustName"
    End Sub
    '' Data being returned from the Linq query
    Public Class ReturnData
        Public Property CustName As String
        Public Property CustomerID As Integer
    End Class

Open in new window

0
 
LVL 62

Accepted Solution

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

Sorry I was working off my iPad when I posted and see I left out a couple of periods in the Select clause as well as the parameter name in the class. Try this.

Private Sub LoadCustomers()
    Dim Cs As List(Of ReturnData) = (From c In db.tblCustomers _
                                     Order By c.CustomerID _
                                     Select New ReturnData With _
                                     { _
                                         .CustName = c.FirstName & " " & c.LastName, _
                                         .CustomerID = c.CustomerID _
                                     } ).ToList()
    
    Dim newReturnData As New ReturnData() With {.CustName = "-all", .CustomerID = 0 }
    Cs.Insert(0, newReturnData)
    
    cmbCustomer.DataSource = Nothing
    cmbCustomer.DataSource = Cs
    cmbCustomer.DisplayMember = "CustomerID"
    cmbCustomer.ValueMember = "CustName"
End Sub
    
'' Data being returned from the Linq query
Public Class ReturnData
    Public Property CustName As String
    Public Property CustomerID As Integer
End Class

Open in new window

0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
ID: 40475193
Magical.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40475196
Glad that did it for you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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