?
Solved

Help to write LINQ query

Posted on 2011-09-27
4
Medium Priority
?
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

770 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