0

Has anyone been able to solve this issue? I've scoured the Internet, and nobody seems to have the correct answer.

Found a similar question here, but everyone seems to give answers related to CAML: Create a dynamic WHERE clause with LINQ to SharePoint

I'm already using Dynamic LINQ: https://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

I also use dynamic sorting on grid view headers, so Dynamic LINQ is necessary.

The problem is building a dynamic where clause with multiple conditional (meaning only add the clause if a drop-down selection is made) AND statements using LINQ.

2
  • Did you check this Unfortunately I am not sure whether it will still work in higher version of on-premise SharePoint i.e. 2016 or 2019 but you could try Commented Apr 5, 2023 at 2:36
  • There's nothing in this that addresses the question.
    – jpollar
    Commented Apr 6, 2023 at 3:09

2 Answers 2

0

I figured it out and forgot to post the answer for future reference.

Using Dynamic LINQ, you can conditionally add more clauses like the following function below.

I'm checking each drop-down selection and ensuring the where clause is empty before appending the additional clause.

Each time a selection is made from any of my drop-down lists, the function is called.

The dynamic LINQ query then repopulates the grid view.

void filterGrid()
    {
        SPSecurity.RunWithElevatedPrivileges(delegate ()
        {
            PortalDataContext db = new PortalDataContext(@"SQLServer");

            if (ddlCerts.SelectedItem.Text == string.Empty && ddlCertType.SelectedItem.Text == string.Empty && ddlPrograms.SelectedItem.Text == string.Empty && ddlCustomers.SelectedValue == string.Empty)
            {
                var qryEmpSearch = from c in db.vw_EmployeeSearches
                                   orderby c.LastName
                                   select c;

                gvEmpSearch.DataSource = qryEmpSearch;

            }
            else
            {
                if (ddlCerts.SelectedValue != string.Empty)
                    strEmpSearchWhere = "cert_name == \"" + ddlCerts.SelectedValue + "\"";

                if (ddlPrograms.SelectedValue != string.Empty)
                {
                    if (!String.IsNullOrEmpty(strEmpSearchWhere))
                        strEmpSearchWhere += " And ";
                    strEmpSearchWhere += "program_name == \"" + ddlPrograms.SelectedValue + "\"";
                }

                if (ddlCustomers.SelectedValue != string.Empty)
                {
                    if (!String.IsNullOrEmpty(strEmpSearchWhere))
                        strEmpSearchWhere += " And ";
                    strEmpSearchWhere += "cust_name == \"" + ddlCustomers.SelectedValue + "\"";
                }

                if (ddlCertType.SelectedValue != string.Empty)
                {
                    if (!String.IsNullOrEmpty(strEmpSearchWhere))
                        strEmpSearchWhere += " And ";
                    strEmpSearchWhere += "cert_type_name == \"" + ddlCertType.SelectedValue + "\"";
                }

                ViewState["strEmpSearchWhere"] = strEmpSearchWhere;

                var qryEmpSearch = from c in db.vw_EmployeeSearches.Where(strEmpSearchWhere)
                                   orderby c.LastName
                                   select c;

                gvEmpSearch.DataSource = qryEmpSearch;

            }

            gvEmpSearch.DataBind();

        });
    }
-1

You can refer to the following article to use LINQ

https://copyprogramming.com/howto/how-to-dynamically-add-where-clause-in-linq

1
  • None of these examples address the question/problem.
    – jpollar
    Commented Apr 6, 2023 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.