locked
How to compose the linq query with lambda RRS feed

  • Question

  • User1183902823 posted

    how to convert the below one with lambda?

    if author name is empty then author filter will not be applied rather all author will be return. tell me how could i convert it with lambda?

    IQueryable<Book> results = from o in _context.Books
                              orderby o.Id
                              select o;
                if (!string.IsNullOrEmpty(author))
                {
                    results = results.Where(t => t.Authers.Contains(author));
                }

    Thursday, December 14, 2017 6:27 PM

Answers

  • User2103319870 posted

    tridip1974

    if author name is empty then author filter will not be applied rather all author will be return. tell me how could i convert it with lambda

    You can try with below query. Logic is OR(||) operator will not check the second condition if first condition satisfies, so you will get all results. If value is not null then second condition will evaluates then you will results which contains the author name

     results = results.Where(t => (author == null || t.Authers.Contains(author)));
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 14, 2017 7:11 PM
  • User2103319870 posted

    IQueryable<Book> results = from o in _context.Books orderby o.Id select o; if (!string.IsNullOrEmpty(author)) { results = results.Where(t => t.Authers.Contains(author)); }

    You can also combine both queries like below

                List<Book> results = _context.Books
                                     .Where(t => (author == null || t.Authers.Contains(author)))
                                     .OrderBy(i => i.Id).ToList();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 14, 2017 7:21 PM

All replies

  • User2103319870 posted

    tridip1974

    if author name is empty then author filter will not be applied rather all author will be return. tell me how could i convert it with lambda

    You can try with below query. Logic is OR(||) operator will not check the second condition if first condition satisfies, so you will get all results. If value is not null then second condition will evaluates then you will results which contains the author name

     results = results.Where(t => (author == null || t.Authers.Contains(author)));
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 14, 2017 7:11 PM
  • User2103319870 posted

    IQueryable<Book> results = from o in _context.Books orderby o.Id select o; if (!string.IsNullOrEmpty(author)) { results = results.Where(t => t.Authers.Contains(author)); }

    You can also combine both queries like below

                List<Book> results = _context.Books
                                     .Where(t => (author == null || t.Authers.Contains(author)))
                                     .OrderBy(i => i.Id).ToList();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 14, 2017 7:21 PM