Here's an example of how you can modify your SendEmail method
private async Task SendEmail(IEnumerable<OrderEmailDto> result)
{
// Grouping the orders based on the order number
var groupedOrders = result.GroupBy(o => o.OrderNo);
foreach (var orderGroup in groupedOrders)
{
var sb = new StringBuilder();
sb.AppendLine($"Dear {orderGroup.First().OrderBy},");
sb.AppendLine("<br />");
sb.AppendLine("<br />");
sb.AppendLine("Your order has been successfully completed. Thank you for choosing us.");
sb.AppendLine("<br />");
sb.AppendLine("<br />");
sb.Append("<table border='1'>");
foreach (var code in orderGroup)
{
sb.Append("<tr>");
sb.Append($"<td>Product: <b>{code?.ProductCode}</b></td>");
sb.Append($"<td>Description: <b>{code?.ProductDescription}</b></td>");
sb.Append($"<td>Pin: <b>{code?.Pin}</b></td>");
sb.Append($"<td>Serial: <b>{code?.Serial}</b></td>");
sb.Append("</tr>");
}
sb.Append("</table>");
sb.AppendLine("<br />");
// ...
// Send the email here. You need the email of the recipient and the email content.
// You can get the recipient's email from any object in the orderGroup.
string recipientEmail = orderGroup.First().Email;
string emailContent = sb.ToString();
// Send the email based on your preferred way (SMTP, MailKit, SendGrid, etc.)
// await SendTheEmail(recipientEmail, emailContent);
}
}