Solved

LINQ, union query, vb.net

Posted on 2014-12-01
19
557 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 63

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 63

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 63

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 63

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 63

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 63

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 63

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 34

Author Closing Comment

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

Expert Comment

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

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 42
how to hit button in form and close application from main() 4 23
Easy filter aspnet 2 27
TFS 2015 Access denied. 1 16
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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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