Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the "Except" operator in the following code. It returns the differences fine, but all I'm getting is the PK_PhoneTypeID (of course). I need to populate a Drop-Down List with "PhoneTypes". I cant figure out how to return this column with my "Except" query, or how to nest another query to return the "PhoneTypes" column.

This is basically what the tables look like:

Phones
PK_ID
FK_ContactID
FK_PhoneTypeID

PhoneTypes
PK_PhoneTypeID
PhoneTypes

Here is my "Except" code and ddl binding:

VB
Dim hiddenGUID As HiddenField = CType(Wizard1.FindControl("Wizardstep3$HiddenField1"), HiddenField)

        Dim dc As New DataClassesDataContext()

        Dim q = (From x In dc.PhoneTypes _
                Select x.PhoneTypeID).Except _
                    (From c In dc.Phones _
                    Where c.ContactID.ToString() = hiddenGUID.Value _
                    Select c.PhoneTypeID)


        'The two commented lines below is what I'm trying to do

        DropDownList2.DataSource = q
        'DropDownList2.DataTextField = "PhoneTypes"
        'DropDownList2.DataValueField = "PhoneTypeID"
        DropDownList2.DataBind()
Posted

Writing a sql statement to do this was easy, so I decided to use the statement directly in Linq to SQL

VB
Dim q As IEnumerable(Of PhoneType) = _
        dc.ExecuteQuery(Of PhoneType) _
                ("select pt.PhoneTypeID, pt.PhoneTypes " & _
                "from PhoneTypes as pt " & _
                "where Not exists " & _
                "(select PhoneTypeID " & _
                "from phones " & _
                "where PhoneTypeID = pt.PhoneTypeID and ContactID ='" + hiddenGUID.Value + "') " & _
                "order by pt.PhoneTypeID;")
        DropDownList2.DataSource = q
        DropDownList2.DataTextField = "PhoneTypes"
        DropDownList2.DataValueField = "PhoneTypeID"
        DropDownList2.DataBind()
 
Share this answer
 
I Kept playing around and found a pure Linq to SQL solution:

SQL
Dim q = From pt In dc.PhoneTypes _
 Where Not (Not (From phones In dc.Phones _
 Where phones.PhoneTypeID = pt.PhoneTypeID And phones.ContactID = ContactGUID _
 Select New With {phones.PhoneTypeID}).Single() Is Nothing) _
 Select pt.PhoneTypeID, pt.PhoneTypes


I hope this helps someone...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900