using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Where_MethodGroup
{
public delegate List<int> WhereDelegate(List<int> list);
class Program :IEnumerable
{
static void Main(string[] args)
{
List<int> list = new List<int>();
list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
WhereDelegate lessThanFive;
lessThanFive = GroupConversionMethod;
IEnumerable<int> query = list.Where(lessThanFive);
foreach (int i in query)
{
Console.WriteLine(i);
}
}
public static List<int> GroupConversionMethod(List<int> list1)
{
Console.WriteLine("Integers less than 5 are :");
foreach (int i in list1)
{
if (i < 5)
{
yield return i;
}
}
}
}
}
我必須通過在where方法中傳遞委託對象來查找少於五個的所有元素。在定義委託對象時,使用要使用的回調函數的方法組轉換語法(通過新運算符定義委託對象)。在Where方法中傳遞委託對象
我在該行收到錯誤:
IEnumerable<int> query = list.Where(lessThanFive)
但如方法有一些無效的參數,這可能是此錯誤的合理解決?
我在IEnumerable行中發現錯誤 query = list.Where(lessThanFive)that方法有一些無效參數,可能是這個錯誤的合理解決方法? –
Ananya
其中,Method是Linq名稱空間中定義的擴展方法,它接收Func類型對象。你不能通過你自己的委託類型 – Saurav