I have a program that displays output in XML format on console windows, that runs perfectly fine without any error, but I am using the Extension Method to do the job. How to do this without using the extension method, I Have a hint that I just need to move one piece of line from extension class to program class but as a new programmer I failed and need your help.
Here is my Main 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();
Here is my Extension Method 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));
}
If I understand correctly, you're asking how to move your extension code into your calling code. Here's how it would look like:
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();