Solved

Help to write LINQ query

Posted on 2011-09-27
4
272 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
Comment Utility
I think your selection filter must be enclosed in single quotes

i.e.

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

Author Comment

by:ConnieLee123
Comment Utility
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
Comment Utility
>"s.DeptName = 'Accounting' AND"

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

Author Comment

by:ConnieLee123
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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:…
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

14 Experts available now in Live!

Get 1:1 Help Now