Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LINQ, union query, vb.net

Posted on 2014-12-01
19
Medium Priority
?
682 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 64

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 34

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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 34

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 64

Expert Comment

by:Fernando Soto
ID: 40474787
Hi eghtebas;

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

Thanks.
0
 
LVL 34

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 34

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 64

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
 
LVL 64

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 34

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 34

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 64

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 34

Author Comment

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

Expert Comment

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

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 34

Author Closing Comment

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

Expert Comment

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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

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…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

963 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