{"id":305,"date":"2013-08-08T11:07:03","date_gmt":"2013-08-08T15:07:03","guid":{"rendered":"http:\/\/binaryworld.net\/blogs\/?p=305"},"modified":"2013-08-13T14:51:10","modified_gmt":"2013-08-13T18:51:10","slug":"how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch","status":"publish","type":"post","link":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/","title":{"rendered":"How to allow comma seperated values for search parameter in LightSwitch"},"content":{"rendered":"<p>Recently someone asked me how to pass multiple values as search parameter in <a title=\"Learn about LightSwitch\" href=\"http:\/\/msdn.microsoft.com\/en-us\/vstudio\/gg604823\" target=\"_blank\">LightSwitch<\/a> ? Well I tried to do some research and looks like there is no in-built control to select multiple items from Filter Dropdown. But after doing some research I found an easy way to accomplish similar solution which is not exactly the way I wanted but it works for me.. and hope it works for you too.<\/p>\n<p>So trick is use TextBox for Search parameter where user can enter multiple comma separated values (e.g. P112,P111,P118)<\/p>\n<p>There are 3 Basic Steps we have to do so we can achieve desired behavior<\/p>\n<ul>\n<li>Create custom query for datasource where you have to define parameter (e.g. CustomerIDList)<\/li>\n<li>Write code to change default Linq query in MyQuery_PreprocessQuery event handler<\/li>\n<li>Create search screen and select Custom Query as data source<\/li>\n<\/ul>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Create_custom_query_and_define_parameter\"><span class=\"toc_number toc_depth_1\">1<\/span> Create custom query and define parameter<\/a><\/li><li><a href=\"#Write_code_in_MyQuery_PreprocessQuery_event_handler\"><span class=\"toc_number toc_depth_1\">2<\/span> Write code in MyQuery_PreprocessQuery event handler<\/a><\/li><li><a href=\"#Create_search_screen_and_use_CustomQuery_as_data_source\"><span class=\"toc_number toc_depth_1\">3<\/span> Create search screen and use CustomQuery as data source<\/a><\/li><li><a href=\"#Run_application_and_test_filter\"><span class=\"toc_number toc_depth_1\">4<\/span> Run application and test filter<\/a><\/li><\/ul><\/div>\n<h2><span id=\"Create_custom_query_and_define_parameter\">Create custom query and define parameter<\/span><\/h2>\n<p>This post assumes you already have defined LightSwitch Datasource. If you already have datasource then you can right click on any datatable and select create new query. Name your new query (e.g. CustomerSearch for this example)<br \/>\nOn query screen define parameter as below (Don&#8217;t bind with search filter).<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg\" alt=\"LightSwitch Create Custom Query\" \/><\/p>\n<h2><span id=\"Write_code_in_MyQuery_PreprocessQuery_event_handler\">Write code in MyQuery_PreprocessQuery event handler<\/span><\/h2>\n<p>Double click on custom query to open Parameter screen. On this screen you will see &#8220;Write Code&#8221; button on you top right corner (see below screen). If you pull up that dropdown you will see various events you can handle for this custom query. Select PreprocessQuery Event.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-eventhandler.jpg\" alt=\"LightSwitch PreprocessQuery Event Handler for Custom Query\" \/><\/p>\n<p>Now what we need to do is modify default Linq query as below so it only selects country found from user defined comma separated list.<\/p>\n<pre class=\"brush: vbnet; gutter: true; first-line: 1; highlight: []; html-script: false\">Namespace LightSwitchApplication\r\n    Public Class NorthwindDataService\r\n\r\n        Private Sub CustomerSearch_PreprocessQuery(CountryList As String, ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.Customer))\r\n            If String.IsNullOrWhiteSpace(CountryList) Then\r\n                CountryList = String.Empty\r\n            End If\r\n\r\n            Dim myInClause() As String = CountryList.Split(&quot;,&quot;)\r\n            For i = 0 To myInClause.Length - 1\r\n                myInClause(i) = myInClause(i).Trim()\r\n            Next\r\n\r\n            query = From cust In query\r\n                    Where myInClause.Contains(cust.Country)\r\n                    Select cust\r\n        End Sub\r\n    End Class\r\n\r\nEnd Namespace<\/pre>\n<h3>Example-2: Multiple parameters<\/h2>\n<p>Here is another example of query with multiple parameters<\/p>\n<pre class=\"brush: vbnet; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n        Private Sub OrdersSearchQuery1_PreprocessQuery(pCustomerList As String, pStartDate As System.Nullable(Of Date), pEndDate As System.Nullable(Of Date), pEmployeeID As System.Nullable(Of Integer), ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.Order) _\r\n                                                      )\r\n            If String.IsNullOrWhiteSpace(pCustomerList) Then\r\n                pCustomerList = String.Empty\r\n            End If\r\n\r\n            Dim myInClause() As String = pCustomerList.Split(&quot;,&quot;)\r\n\r\n            For i = 0 To myInClause.Length - 1\r\n                myInClause(i) = myInClause(i).Trim()\r\n            Next\r\n\r\n            query = From o In query\r\n                    Where (myInClause.Contains(o.Customer1.CustomerID) _\r\n                           OrElse pCustomerList = String.Empty) _\r\n                       AndAlso (o.OrderDate &gt;= pStartDate AndAlso o.OrderDate &lt;= pEndDate) _\r\n                       AndAlso (o.Employee.EmployeeID = pEmployeeID)\r\n                    Select o\r\n        End Sub\r\n<\/pre>\n<h2><span id=\"Create_search_screen_and_use_CustomQuery_as_data_source\">Create search screen and use CustomQuery as data source<\/span><\/h2>\n<p>Once we have custom query defined we can use it to bind with search screen.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-searchscreen.jpg\" alt=\"LightSwitch Search Screen and Databinding\" \/><\/p>\n<p>Create new search screen and select CustomQuery we created as datasource. Below screenshot shows new screen designtime properties.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-search-screen-design.jpg\" alt=\"LightSwitch Designer for Search Screen\" \/><\/p>\n<h2><span id=\"Run_application_and_test_filter\">Run application and test filter<\/span><\/h2>\n<p>Now execute application and try to search by putting some comma separated values and see your filter works.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-run-application.jpg\" alt=\"LightSwitch Test Multi Value Filter\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently someone asked me how to pass multiple values as search parameter in LightSwitch ? Well I tried to do some research and looks like there is no in-built control to select multiple items from Filter Dropdown. But after doing &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/\"> <span class=\"screen-reader-text\">How to allow comma seperated values for search parameter in LightSwitch<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":314,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[162],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>How to allow comma seperated values for search parameter in LightSwitch - BinaryWorld Blog<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"How to allow comma seperated values for search parameter in LightSwitch - BinaryWorld Blog\" \/>\r\n<meta property=\"og:description\" content=\"Recently someone asked me how to pass multiple values as search parameter in LightSwitch ? Well I tried to do some research and looks like there is no in-built control to select multiple items from Filter Dropdown. But after doing &hellip; How to allow comma seperated values for search parameter in LightSwitch Read More &raquo;\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/\" \/>\r\n<meta property=\"og:site_name\" content=\"BinaryWorld Blog\" \/>\r\n<meta property=\"article:published_time\" content=\"2013-08-08T15:07:03+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2013-08-13T18:51:10+00:00\" \/>\r\n<meta property=\"og:image\" content=\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg\" \/>\r\n\t<meta property=\"og:image:width\" content=\"589\" \/>\r\n\t<meta property=\"og:image:height\" content=\"331\" \/>\r\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\r\n<meta name=\"author\" content=\"Binary World\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Binary World\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/\",\"url\":\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/\",\"name\":\"How to allow comma seperated values for search parameter in LightSwitch - BinaryWorld Blog\",\"isPartOf\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg\",\"datePublished\":\"2013-08-08T15:07:03+00:00\",\"dateModified\":\"2013-08-13T18:51:10+00:00\",\"author\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\"},\"breadcrumb\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#primaryimage\",\"url\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg\",\"contentUrl\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg\",\"width\":\"589\",\"height\":\"331\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/binaryworld.net\/blogs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to allow comma seperated values for search parameter in LightSwitch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\",\"url\":\"https:\/\/binaryworld.net\/blogs\/\",\"name\":\"BinaryWorld Blog\",\"description\":\"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\",\"name\":\"Binary World\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"caption\":\"Binary World\"},\"description\":\"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.\",\"url\":\"https:\/\/binaryworld.net\/blogs\/author\/admin\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to allow comma seperated values for search parameter in LightSwitch - BinaryWorld Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/","og_locale":"en_US","og_type":"article","og_title":"How to allow comma seperated values for search parameter in LightSwitch - BinaryWorld Blog","og_description":"Recently someone asked me how to pass multiple values as search parameter in LightSwitch ? Well I tried to do some research and looks like there is no in-built control to select multiple items from Filter Dropdown. But after doing &hellip; How to allow comma seperated values for search parameter in LightSwitch Read More &raquo;","og_url":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/","og_site_name":"BinaryWorld Blog","article_published_time":"2013-08-08T15:07:03+00:00","article_modified_time":"2013-08-13T18:51:10+00:00","og_image":[{"width":"589","height":"331","url":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg","type":"image\/jpeg"}],"author":"Binary World","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Binary World","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/","url":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/","name":"How to allow comma seperated values for search parameter in LightSwitch - BinaryWorld Blog","isPartOf":{"@id":"https:\/\/binaryworld.net\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#primaryimage"},"image":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#primaryimage"},"thumbnailUrl":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg","datePublished":"2013-08-08T15:07:03+00:00","dateModified":"2013-08-13T18:51:10+00:00","author":{"@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0"},"breadcrumb":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#primaryimage","url":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg","contentUrl":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/08\/lightswitch-create-custom-query.jpg","width":"589","height":"331"},{"@type":"BreadcrumbList","@id":"https:\/\/binaryworld.net\/blogs\/how-to-allow-comma-seperated-values-for-search-parameter-in-lightswitch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/binaryworld.net\/blogs\/"},{"@type":"ListItem","position":2,"name":"How to allow comma seperated values for search parameter in LightSwitch"}]},{"@type":"WebSite","@id":"https:\/\/binaryworld.net\/blogs\/#website","url":"https:\/\/binaryworld.net\/blogs\/","name":"BinaryWorld Blog","description":"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0","name":"Binary World","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","caption":"Binary World"},"description":"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.","url":"https:\/\/binaryworld.net\/blogs\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/305"}],"collection":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":0,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media\/314"}],"wp:attachment":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}