none
LINQ PredicateBuilder not work in my dynamic search RRS feed

  • Question

  • Hello.

    I have a book table in my database and I have book view named vwShared in my edmx. I want to create dynamic search with operators for user to find books. I have 2 SearchColumns dropdownlist contains "Title, Authors, Published Year, Subject". I have 2 SearchType dropdownlist contains "StartsWith, Contains, EndsWith, Equals". I have another dropdownlist contains "AND, OR" to combine 2 search results. The following is my code.

     var predicate = PredicateBuilder.True<DataLayer.vwShared>();

    if (joinOperator == "AND")

    {

                            if (SearchColumn1 == "Title" && SearchType1 == "Contains")
                                predicate = predicate.And(e1 => e1.Title.Contains(txtSearch1.Text));
                            if (SearchColumn2 == "Authors" && SearchType2 == "Contains")
                                predicate = predicate.And(e1 => e1.Authors.Contains(txtSearch2.Text));
    }

    else if (joinOperator == "OR")

    {

       if (SearchColumn1 == "Title" && SearchType1 == "Contains")
                                predicate = predicate.Or(e1 => e1.Title.Contains(txtSearch1.Text));
                            if (SearchColumn2 == "Authors" && SearchType2 == "Contains")
                                predicate = predicate.Or(e1 => e1.Authors.Contains(txtSearch2.Text));

    }

                                List<DataLayer.vwShared> bookList= new DataLayer.Solib_DMREntities().SP_SharedData_GetAll("AllLocal").ToList<DataLayer.vwShared>();

                                var bookList= from books in bookList.AsQueryable().Where(predicate)
                                                 select books ;

    gvBooks.DataSource = bookList.ToList();

    gvBooks.DataBind();

    The above code not return proper results. Is there something wrong. ?

    The following is my references website.

    http://www.albahari.com/nutshell/predicatebuilder.aspx

    Please give me advice.

    Thanks.

    Monday, August 22, 2016 7:08 AM

Answers

  • Hi nyinyiaung,

    According to your code, please check the following code, which you define variable named booklist twice.

    List<DataLayer.vwShared> bookList= new DataLayer.Solib_DMREntities().SP_SharedData_GetAll("AllLocal").ToList<DataLayer.vwShared>();
    
    var bookList= from books in bookList.AsQueryable().Where(predicate)
                                                  select books ;

    Please modify it like this:

    List<DataLayer.vwShared> bookList= new DataLayer.Solib_DMREntities().SP_SharedData_GetAll("AllLocal").ToList<DataLayer.vwShared>();
    
    var query = from books in bookList.AsQueryable().Where(predicate)
                                                  select books ;

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 23, 2016 1:47 AM
    Moderator