7

I have list with empty space("__")

List<string> MyList = (List<string>)Session["MyList "];

if(MyList !=null || MyList != "")
{
}

MyList != "" does not work if string has more space so

How can i check my list string is "" or null by using linq in c# ?

4
  • A list will never be "". What is it you want to check exactly? Commented Oct 13, 2013 at 11:38
  • Never say never . My List is "" sometimes :)) Commented Oct 13, 2013 at 11:42
  • 1
    @JeroenVannevel List would never be empty string but Session["MyList "] might be just that, guess that's what the OP means. Commented Oct 13, 2013 at 11:43
  • Not duplicate , because this is list other one is only string. Commented Oct 13, 2013 at 11:48

3 Answers 3

19
if(MyList!=null || MyList.All(x=>string.IsNullOrWhiteSpace(x)))
{

}
Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

if(MyList.All(s=>string.IsNullOrWhiteSpace(s)))
{
      ....
}

Comments

1
var emptyStrings = MyList.Where(p => string.IsNullOrWhiteSpace(p)).ToList();
var listWithoutEmptyStrings = MyList.Where(p => string.IsNullOrWhiteSpace(p)).ToList();

If you just want to check if the list contains one ore more such items:

if (MyList.Any(p => string.IsNullOrWhiteSpace(p)))
{
}

If you want to check if all elements are null or empty

if (MyList.All(p => string.IsNullOrWhiteSpace(p)))
{
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.