Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

LINQ, union query, vb.net

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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

Avatar of Mike Eghtebas

ASKER

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.
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
Walter, I got an error:
Error      1      Type or 'With' expected.      

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

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

Thanks.
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
I don't know the exactly sintax in VB so i use C# (in bold) and you translate the sentences
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)'.
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

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)'.
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.
Hi Fernando,

There are two errors (one is repeat error on Cs). Please see this image for their locations and descriptions.User generated image
On the last line of the query you are missing the, ),  it should be as follows.

C.CustomerID } ).ToList()
I added )
The same errors still show on the same spots.
Please repost the code. Thanks.
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

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Magical.
Glad that did it for you.