1

I’m using a dictionary and the key will be the property name and value will be the LINQ query below:

Model.Listexample.Select(a => a.property)

How I can check if there are any items in the list without writing if statement? We can use any other LINQ statement where it checks if there are any elements in the list before using Select().

I tried (Model.Listexample.Any()).Select but it didn’t work.

3
  • 2
    And how do you think Any() is written? What do you have against if() statements? Commented Mar 29, 2019 at 5:18
  • Hi Tanveer - I think it’s not good to use an if statement inside a dictionary object. As I mentioned above key is property name and value is Select LINQ statement, I want to use Any() and Select() in a same statement rather than using if statement again. Is it possible through LINQ? Commented Mar 29, 2019 at 5:23
  • @Stackoverflowuser - Could you please clarify your question as per the comments in the answers? Commented Mar 29, 2019 at 6:12

3 Answers 3

4

Don't check if the List is empty, the Select will do it for you. At most you can check for null:

var newList = Model.Listexample?.Select(a => a.property)
Sign up to request clarification or add additional context in comments.

9 Comments

As commented, the OP does not need to check the list for nullity but for emptiness
@vc74 - The OP's question doesn't make sense if taken at face value - this answer is the best interpretation of the actual need in my opinion.
@Enigmativity IMO it's not, the OP has commented that the list is not null. He/she just want to have a different behavior in case the list is empty. Question is... which?
@Stackoverflowuser The point that I am trying to make in my answer is that the Select statement does check for Empty and returns an empty List. Is there something else that you want to do in that case?
@vc74 this sounds to me like an XY problem
|
0

You can check to use below syntax in linq:

Model.Listexample?.Select(a => a.property)

Comments

-1
Model.Listexample.Any(a => a.property) ? Model.Listexample.Select(a => a.property) : null;

5 Comments

Hi meJustAndrew and Bilal- I want to check if list has any items in it specifically it shouldn’t be an empty list like []. null check is already validated before in the code.
@Stackoverflowuser Which behavior (default value, exception...?) do you want when the list is empty?
@Stackoverflowuser Also, if you want to comment answers please do in the specific answers, not in other answers not related to your comment.
Is it correct ?Model.Listexample.Any(a => a.property) ? Model.Listexample.Select(a => a.property) : null;
@Stackoverflowuser It can be correct if a.property is a boolean, and you want to check if it's true for any item in the list but if you just want to check whether there are items in the list: Any() will do (check my answer)

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.