In C#, why can't an anonymous method contain a yield statement?

前端 未结 5 789
青春惊慌失措
青春惊慌失措 2020-11-30 19:23

I thought it would be nice to do something like this (with the lambda doing a yield return):

public IList Find(Expression

        
5条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 20:06

    I would do this:

    IList list = GetList();
    var fun = expression.Compile();
    
    return list.Where(item => fun.Invoke(item)).ToList();
    

    Of course you need the System.Core.dll referenced from .NET 3.5 for the Linq method. And include:

    using System.Linq;
    

    Cheers,

    Sly

提交回复
热议问题