如何在没有扩展方法的情况下做同样的事情

How to do same without Extension Method

我有一个程序在控制台 windows 上以 XML 格式显示输出,它运行得很好,没有任何错误,但我正在使用扩展方法来完成这项工作。如何在不使用扩展方法的情况下做到这一点,我有一个提示,我只需要将一行从扩展 class 移动到程序 class 但是作为一个新程序员我失败了,需要你的帮助。

这是我的主Class

List<int> email = new List<int>() { 60, 50, 70, 30, 80, 65, 90, 75, 55 };

        var element = new XElement("Results",
            email.Batch(3)
                 .Select(batch =>
                     new XElement("Result",
                                  batch.Select(mark => new XElement("Mark", mark)),
                                  new XElement("Total", batch.Sum()))));
        Console.WriteLine(element);



        Console.ReadLine();

这是我的扩展方法class

 public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items,
                                                  int maxItems)
    {
        return items.Select((item, inx) => new { item, inx })
                    .GroupBy(x => x.inx / maxItems)
                    .Select(g => g.Select(x => x.item));
    }

如果我没理解错的话,你问的是如何将你的扩展代码移动到你的调用代码中。这是它的样子:

        List<int> email = new List<int>() { 60, 50, 70, 30, 80, 65, 90, 75, 55 };

        var element = new XElement("Results",
            email
                .Select((item, inx) => new { item, inx })
                .GroupBy(x => x.inx / 3)
                .Select(g => g.Select(x => x.item))                
                .Select(batch =>
                    new XElement("Result",
                        batch.Select(mark => new XElement("Mark", mark)),
                        new XElement("Total", batch.Sum())
                    )
                )
        );
        Console.WriteLine(element);
        Console.ReadLine();