Solved

LINQ, union query, vb.net

Posted on 2014-12-01
19
514 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi eghtebas;

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

Thanks.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I added )
The same errors still show on the same spots.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Please repost the code. Thanks.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
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
Comment Utility
Magical.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Glad that did it for you.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

11 Experts available now in Live!

Get 1:1 Help Now