Mych
Mych

Reputation: 2553

Dynamically build a LINQ statement

I am trying to build a LINQ query dynamically. In this example I have one of two locations for the value of one of the fields I thought I could do something like this....

Dim newCompany = New IOCompany With {.Company = PCase(cmbx_Company.SelectedValue.Trim), _
                                     If cmb_CompanySchedule.SelectedIndex = 0 Then _
                                         .CompanySchedule = "StdSchedule", _
                                     Else
                                         .CompanySchedule = cmb_CompanySchedule.SelectedIndex, _
                                     End If _
                                    }

db.IOCompanies.InsertOnSubmit(newCompany)
db.SubmitChanges()
Result = newCompany.CID

But of course this is wrong... How can I do something like this... Thanks

Upvotes: 0

Views: 62

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

This has nothing to do with LINQ.

Just don't use With for CompanySchedule:

Dim newCompany = New IOCompany With {.Company = PCase(cmbx_Company.SelectedValue.Trim) }
If cmb_CompanySchedule.SelectedIndex = 0 Then
    newCompany.CompanySchedule = "StdSchedule"
Else
    newCompany.CompanySchedule = cmb_CompanySchedule.SelectedIndex
End If


db.IOCompanies.InsertOnSubmit(newCompany)
db.SubmitChanges()
Result = newCompany.CID

Upvotes: 1

Related Questions