Display a single message with records when Id exists in C# Linq -


i have check record if exists display message id stating following id's exists. have written message in loop message repeated. if write console message outside loop scope ends.

current output :

cannot delete id 1 exists.
cannot delete id 2 exists.

required output :

cannot delete 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 {0} exists",c.id));                 }          }          console.readline();     } } 

use this

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 });         list<int> lst = new list<int>();         foreach (var c in client)         {             var r = server.any(x => x.id == c.id);             if (r==true)             {                 lst.add(c.id);              }          }          if(lst.count() > 0)             console.writeline(string.format("cannot delete {0} exists",string.join(",",lst)));              console.readline();     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -