locked
perform search using linq RRS feed

  • Question

  • User585649674 posted

    user can input full or partial term separated by commas like orange,app*,gra*

    how can i use linq to get the results, I tried something like below, which will not work as it is adding & condition between each term

    IQueryable<fruits> fruitlistQuery = FruitRepository.GetAll();

    for (int count = 0; count < SearchRequest.Fruits.Length; count++)
    {
    string search = SearchRequest.LotNo[count].Replace("*", "");
    fruitlistQuery  = fruitlistQuery .Where(x => x.lfuitname.Contains(search));
    }

    i even tried like this, but it did not work for partial terms.

    fruitlistQuery = fruitlistQuery .Where(x => SearchRequest.Fruits.Contains( x.lfuitname));

    Saturday, February 13, 2016 2:02 PM

Answers

  • User585649674 posted

    inside each for loop the result is initialized, i guess you wanted this result to be added to another resultset. I also have other criterias.

    I solved the problem using LinqKit, the answer is as follows

    if (SearchRequest.fruits.Length != 0)
    {
    var predicate = PredicateBuilder.False<Lot>();
    for (int count = 0; count < SearchRequest.fruits.Length; count++)
    {
    string search = SearchRequest.fruits[count].Replace("*", "");
    predicate = predicate.Or(n => n.fruits.Contains(search));
    }
    fruitlistQuery = fruitslistQuery.AsExpandable().Where(predicate);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2016 10:12 AM

All replies

  • User-698989805 posted

    I hope, you are trying to do something as follows and it works for the partial values. More specifically the second one:

    string[] str = txtSearch.Text.Split(',');
        foreach (string k in str)
          {
             var result = (from c in context.Customer
                           where c.CustName.Contains(k)
                           select c).ToList();
          }
    
    OR
    
        foreach (string k in str)
          {
             var result = (from c in context.Customer
                           where str.Any(t => c.CustName.Contains(t))
                           select c).ToList();
          }

    First, keep the inputs in an array (string). Then split the comma and iterate through a loop. I am not sure if the star(*) gets replaced in your code. Give a try.

    Sunday, February 14, 2016 8:29 AM
  • User585649674 posted

    inside each for loop the result is initialized, i guess you wanted this result to be added to another resultset. I also have other criterias.

    I solved the problem using LinqKit, the answer is as follows

    if (SearchRequest.fruits.Length != 0)
    {
    var predicate = PredicateBuilder.False<Lot>();
    for (int count = 0; count < SearchRequest.fruits.Length; count++)
    {
    string search = SearchRequest.fruits[count].Replace("*", "");
    predicate = predicate.Or(n => n.fruits.Contains(search));
    }
    fruitlistQuery = fruitslistQuery.AsExpandable().Where(predicate);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2016 10:12 AM
  • User-698989805 posted

    You asked for the partial values to return regarding the query and the query I provided worked fine for the values like orange,app*. I did not care about the star(*) as you were replacing it with another method and you did not mention any other related I mean other criteria. Though glad to know it worked.

    Tuesday, February 16, 2016 6:46 PM