Using StringBuilder to form the query dynamically.
if (!(String.IsNullOrEmpty(implanter)))
{
query[querycount] = "<In><FieldRef Name='Implanter' /><Values>" + implanter + "</Value></In>";
querycount++;
}
Frage
I have a search form which has data in controls like checkboxlist (Multi Select), text box I need to search SharePoint list for the values and display the list records in grid view.
Please suggest me how to write the search query which is dynamic.. Ex: If there are no values selected in one text box.. I need not include it in the query.
Lösung 2
Using StringBuilder to form the query dynamically.
if (!(String.IsNullOrEmpty(implanter)))
{
query[querycount] = "<In><FieldRef Name='Implanter' /><Values>" + implanter + "</Value></In>";
querycount++;
}
Andere Tipps
You can either run SPMetal to generate static objects to query your lists using LINQ
Or just use an old and yet effective CAML Query
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Eq><FieldRef Name='YourField'/>" +
"<Value Type='Text'>Insert value to search here</Value></Eq></Where>";
SPListItemCollection collListItems = oList.GetItems(oQuery);
In a straight forward linq query
var results = MySPList.Items.Cast<SPListItem>()
.Where(SPItem => SPItem["YourField"] == "Your query");
However, running spmetal is the way to go if you plan to use linq queries frequently, because using straight linq queries on splists can result on serious performance issues.