Can I get patience with lambdas?

I am currently writing a lot of code with lambdas.

return _schema.GetAll<Node>() .ToList() .FindAll(node => node.Type == NodeType.Unmanaged) .Cast<Shape>() .ToList(); 

Note. GetAll () returns an IList.

Can I get any translation?

+4
source share
2 answers
  • You can replace ToList and then FindAll with Where.
  • A popular standard with lambda parameters in simple operators is a single character. 'node' can be renamed to 'n'.
  • Your method may return IEnumerable instead of IList. After that, the calling method could call ToList.

After:

 return _schema.GetAll<Node>().Where(n => n.Type == NodeType.Unmanaged).Cast<Shape>(); 
+3
source

That should work.

 return _schema.GetAll<Node>() .Where(node => node.Type == NodeType.Unmanaged) .Cast<Shape>() .ToList() 

If your method had an IEnumerable<Shape> return type, you would not need to call ToList() .

You can also write it like this (with IEnumerable<Shape> return type):

 return from node in _schema.GetAll<Node>() where node.Type == NodeType.Unmanaged select node as Shape; 
+2
source

All Articles