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));
– ramaral
You want to go through and carry out what kind of action?
– Laerte
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 */);`
– Maniero
you could post a full example using for, just so we can understand what you want to achieve using?
– Tobias Mesquita
I think what you want to do is a search and not perform an action and for that you need to make a
Where
. TheForEach
serves to perform actions for each iterated item. That isvlstDados.Where(l => l.IcObrigatorio == 0 && String.IsNullOrEmpty(l.DsPathDocumento))
– Ninita
@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
– pnet
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
@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
@pnet just now saw your answer using the
All
. But yes, theAll
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 theAny
, but if you want to check that all items meet the condition then you should use theAll
– Ninita