sim.plified.com

Chris Pollock

Chris Pollock - web developer (PHP/mySQL & ASP.NET)
undivided… my thoughts on world, family, church, business, technology and Jesus Christ (all in all)

"WHERE OR" in LINQ using "Contains"

In on of my recent implementations I was looking to loop through a series of categories and then find all the jobs in those categories and add them to a the results list.  The most logical way of doing this would have been to "Where OR" a bunch of items together.  LINQ however does not contain a "WhereOr" operator.  Instead I was able to implement by 1) shoving the selected items into an integer array, and then 2) Using the "Contains" command to filter down the job/category link table.

Here's a code snippet:

            'If the category selection method is selected then filter the jobs.
            If rblCategories.SelectedValue = "Some" Then

                'Get all the categories and throw them into an array.
                Dim CategoryIdList As New ArrayList()
                For Each li As ListItem In cblCategories.Items
                    If li.Selected Then
                        Dim intCategoryId As Integer = li.Value
                        CategoryIdList.Add(intCategoryId)
                    End If
                Next

                'Cast the array list into the an array of integers
                Dim intCategoryIdList As Integer() = CType(CategoryIdList.ToArray(GetType(Integer)), Integer())

                'Filter the category List
                Dim categories = From c In dbCareerServices.LQ_TBL_JOBS_TO_CATEGORies Select c
                categories = categories.Where(Function(ct) intCategoryIdList.Contains(ct.int_category_id))

                'Now filter the job list
                result = result.Where(Function(j) categories.Any(Function(c) c.int_job_id = j.int_job_id))
            End If

 

If anyone knows of a better way to get similar results with LINQ, please let me know.

Technorati Tags: ,,

No Comments, Comment or Ping

Reply to “"WHERE OR" in LINQ using "Contains"”

My Pictures

CIMG6271CIMG6266CIMG6264CIMG6253CIMG6235CIMG6233CIMG6231CIMG6227CIMG6218I think I'm on to something here..

Chris Pollock

Web Developer - proficient in both PHP and ASP.NET.
Rochester, New York

View my web developement site.

View Chris Pollock's LinkedIn profile

My Pictures

CIMG6271CIMG6266CIMG6264CIMG6253CIMG6235CIMG6233CIMG6231CIMG6227CIMG6218I think I'm on to something here..