I want to populate a dropdown with values from database and constant hard-coded literals using linq. For example, assuming I already have northwind datacontext, i want to populate the dropdown with category table and some constants so that the dropdown will have something like below.
value text
0 [All] -1 [None] 1 Beverages 2 Condiments 3 Confections : : 8 Seafood
and so on. First two items are hard-coded constants and the rest are from database using linq.
I tried this, but did not work. Please help. Thanks.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim db As New NorthwindDataContext
Dim q = From c In db.Categories _
Select c.CategoryID, c.CategoryName
Dim q2 = From c In db.Categories _
Select CategoryID = 0, CategoryName = "All"
DropDownList1.DataTextField = "CategoryName"
DropDownList1.DataValueField = "CategoryID"
DropDownList1.DataSource = q.Union(q2)
DropDownList1.DataBind()
End Sub
I want to use LINQ to accomplish this. Yes, you can add to DropdownList but...