Solved

Help to write LINQ query

Posted on 2011-09-27
4
275 Views
Last Modified: 2013-01-04
Hi,
 
  I am new to LINQ and need a LINQ query base on user's selection.  

  'This selection maybe more if user input more filter
  selection = "DeptName = "Accounting" And StudyYear = 2005"

                Dim query = From s In db.vwStudies
                            Where (selection)
                            Select s.StudyID, s.DeptName, s.FirstLastName, s.StudyYear

I got this error message:
Conversion from string "s.DeptName = 'Berman Center' AND" to type 'Boolean' is not valid.


PLEASE HELP!

Thank you so much!
0
Comment
Question by:ConnieLee123
  • 2
4 Comments
 
LVL 6

Expert Comment

by:kswathi
ID: 36712203
I think your selection filter must be enclosed in single quotes

i.e.

 "DeptName = ' "Accounting " ' " And StudyYear = 2005"
0
 

Author Comment

by:ConnieLee123
ID: 36712371
Sorry my typo....  But here is my more detailed code.  I had it right that it was encoused in single quotes.

            If Not ddlDepartment.SelectedItem.ToString = "All Departments" Then
                If Len(selection) = 0 Then
                    selection = selection & "s.DeptName = '" & ddlDepartment.SelectedItem.ToString & "'"
                Else
                    selection = selection & " AND s.DeptName = '" & ddlDepartment.SelectedItem.ToString & "'"
                End If
            End If

            If Not ddlStudyYear.SelectedItem.ToString = "All First Name" Then
                If Len(selection) = 0 Then
                    selection = selection & "StudyYear = " & CType(ddlStudyYear.SelectedItem, Integer)
                Else
                    selection = selection & " AND s.StudyYear = " & CType(ddlStudyYear.SelectedItem, Integer)
                End If
            End If

            End If Dim query = From s In db.vwStudies
                            Where (selection)
                            Select s.StudyID, s.DeptName, s.FirstLastName, s.StudyYear

Got Error:

Conversion from string "s.DeptName = 'Accounting' AND" to type 'Boolean' is not valid.


THANKS!!!!
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36891473
>"s.DeptName = 'Accounting' AND"

Do you see a problem there? a AND without another condition.
0
 

Author Comment

by:ConnieLee123
ID: 36893925
It is the error cut off.  The condition is there.  

I did more research, I need to convert it to dynamic LINQ...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding bootstrap to only Content page and NOT master page 1 50
Where is this file? 3 26
SSIS Start 2 21
IDE for Python 5 25
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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