I have to check record if exists then display a message with Id
stating that following Id's
exists.
As I have written the message in a loop so the message is repeated.
If I write the console message outside the loop then scope ends.
Current output :
Cannot delete as Id 1 exists.
Cannot delete as Id 2 exists.
Required Output :
Cannot delete as Id 1,2 exists.
public class Program
{
public int Id { get; set; }
static void Main(string[] args)
{
List<Program> client = new List<Program>();
client.Add(new Program { Id = 1 });
client.Add(new Program { Id = 2 });
client.Add(new Program { Id = 3 });
client.Add(new Program { Id = 4 });
client.Add(new Program { Id = 5 });
List<Program> server = new List<Program>();
server.Add(new Program { Id = 2});
server.Add(new Program { Id = 4 });
foreach (var c in client)
{
var r = server.Any(x => x.Id == c.Id);
if (r==true)
{
Console.WriteLine(String.Format("Cannot delete as {0} exists",c.Id));
}
}
Console.ReadLine();
}
}