Solved

Help to write LINQ query

Posted on 2011-09-27
4
274 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 42
Example works but why do I need to click twice on button 4 36
VB.NET 2008 Publish Error 2 24
Release Dynamically Allocated Memory in C# 3 20
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now