How to verify that all elements of a list satisfy the same condition using a lambda expression?

Asked

Viewed 423 times

1

I have a class that represents an entity in the database. This class runs a Procedure that returns its data. I created a list of the type and this list is loaded. Let’s assume that this is the list loaded: vlstMinhaEntidade. How do I go through this list with lambda?

Personal, for and foreach, I know that. The question would be with lambda. That way it didn’t work. The goal of this is just learning.

var teste = l in vlstMinhaEntidade...

The way above gives error in everything.

That’s the way you’re blowing it:

vbolErroDocTorObrigatorio = vlstDados.ForEach(
   l => l.IcObrigatorio == 0 && string.IsNullOrEmpty(l.DsPathDocumento)
);
  • I must not have understood the question why List implements this: vlsMinhaEntidade.ForEach(item => qualquerAçao(item));

  • You want to go through and carry out what kind of action?

  • I’ve seen you do it so many times, I thought you already knew. I’m not sure what you’d think but I think it would be something like this: vlsMinhaEntity.Foreach(l => /* does something here */);`

  • you could post a full example using for, just so we can understand what you want to achieve using?

  • 3

    I think what you want to do is a search and not perform an action and for that you need to make a Where. The ForEach serves to perform actions for each iterated item. That is vlstDados.Where(l => l.IcObrigatorio == 0 && String.IsNullOrEmpty(l.DsPathDocumento))

  • @Ninita, it’s no mistake, but is there any way I can set my boolean variable straight? Type(gives error) but the idea would be this: vbolErroDocTorObrigatorio = vlstDados.Where(l => l.Icobrigatorio == 0 && String.Isnullorempty(l.Dspathdocument)); or would have to take the result and an if setar

  • 1

    For this you can use the Any that checks whether there are items that meet the condition: vbolErroDocTorObrigatorio = vlstDados.Any(l => l.IcObrigatorio == 0 && String.IsNullOrEmpty(l.DsPathDocumento)); and so the result is bool

  • @Ninita, thanks. Thanks for the answer. I’ll trade it for Any, but remembering that All didn’t give me a mistake, but I don’t know if it’s working, because I haven’t tested it yet, I’ll do it now.

  • @pnet just now saw your answer using the All. But yes, the All also can work but it all depends on what you need. If you just want to check if there is any item that meets the condition just use the Any, but if you want to check that all items meet the condition then you should use the All

Show 4 more comments

1 answer

1


I did so and settled with All:

vbolErroDocTorObrigatorio = vlstDados.All(l => l.IcObrigatorio == 0 && String.IsNullOrEmpty(l.DsPathDocume

I got the answer in the OS in English.

  • The All method checks for the same condition on all elements of the list. Based on your solution, I suggest that your question be renamed to "How to verify that all elements of a list satisfy the same condition using a lambda expression."

Browser other questions tagged

You are not signed in. Login or sign up in order to post.