Mike Eghtebas
asked on
LINQ, union query, vb.net
The following code works fine.
Question: How can I add Union Select 0, "- all -" to it?
You may find the following link helpful:
http://stackoverflow.com/questions/4751924/linq-union-how-to-add-a-literal-value-to-the-query
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"
You may find the following link helpful:
http://stackoverflow.com/questions/4751924/linq-union-how-to-add-a-literal-value-to-the-query
ASKER
My existing code is:
How would I incorporate this with what you have? I tried something similar but got no where.
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"
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
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
ASKER
Walter, I got an error:
Error 1 Type or 'With' expected.
Cs.Add( new {CustName = "--all--", CustomerID = 0} );
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.
What are you looking to do? Please explain in detail.
Thanks.
ASKER
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
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
ASKER
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)'.
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.
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
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)'.
Error 1 Value of type '<anonymous type> (line 23)' cannot be converted to '<anonymous type> (line 19)'.
ASKER
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.
Thank you for the great help.
ASKER
On the last line of the query you are missing the, ), it should be as follows.
C.CustomerID } ).ToList()
C.CustomerID } ).ToList()
ASKER
I added )
The same errors still show on the same spots.
The same errors still show on the same spots.
Please repost the code. Thanks.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Magical.
Glad that did it for you.
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.
Open in new window