0

A variable posted by user from ui:"_author".
it can be one name or comma seperated more names.. first one is ok but I need to deal with second scenario _author = "Erick";// thats ok

here is fine with single name,

if (!string.IsNullOrEmpty(_author))
      {
          allQueryable = allQueryable.Where(u => u.Authors.Contains(_author));
      }

_author = "Erick,Jennifer,Patrick,..";//hmmm

what I tried for this case:

     if (!string.IsNullOrEmpty(_author)) 
{ 
       if (_author.Contains(',')) 
        {
        allQueryable = allQueryable.Where(u =>u.Authors.Intersect(_author.Split(',').ToArray())); 
       } 
       else 
          allQueryable = allQueryable.Where(u => u.Authors.Contains(_author));

}

Entity defination:

public class Book : EntityBase
    {
        public string Title { get; set; }
        public string Publisher { get; set; }
        public string Description { get; set; }
        public string[] Authors { get; set; }
    }
4
  • 1
    I think you meant to write if(!string.IsNullOrEmpty(_author)). You've excluded the "!" at the start of your condition. Commented Jul 24, 2016 at 2:47
  • 1
    What's your question? Commented Jul 24, 2016 at 2:47
  • so what is your question ? Commented Jul 24, 2016 at 3:07
  • My question is how can achieve my goal the thing above not work guys.. Commented Jul 24, 2016 at 13:30

1 Answer 1

2

You do not need to have a special case for one entry and multiple entries separated by commas, and you don't need to handle the case where the user types nothing. Replace the two nested if statements with these two lines:

string[] authors = _author.Replace(" ","").Split(',');
allQueryable = allQueryable.Where(u => u.Authors.Intersect(authors).Count() > 0).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.