Solved

Help to write LINQ query

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

705 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