c# - How to create a List<class> and verify if list contains item already -
easier if show trying do:
public struct server { public string servername; public string sitename; } list<server> targetserverlist = gettargetserverlist();
targetserverlist have following data:
sitename: servername: 1 sitename: servername: 2 sitename: b servername: 3 sitename: c servername: 4
goal: need create list contain following:
sitename: site servernames: 1,2 sitename: site b servernames: 3 sitename: site c servernames: 4
so created following class:
private class serverspersite { string servernames; string sitename; }
so need loop , add new item new sitename if sitename not exist yet plus add servername it. if exist, need add servername:
foreach (server server in targetserverlist) { //??missing code }
how do that? in javascript simple:
//separate servers per site var sites=new object(); $("#servergrid").jqgrid('getgridparam', 'data').foreach(function(row) { if(!sites[row.sitename]) { sites[row.sitename] = new array(); } sites[row.sitename][sites[row.sitename].length] = row.servername; }
thank you!
in case dictionary<string, string>
or dictionary<string, ilist<string>>
better choice.
public struct server { public string id; public string sitename; } private idictionary<string, ilist<string>> servers; foreach(var server in gettargetserverlist()){ if(!servers.containskey(server.sitename) { servers.add(server.sitename, new list<string>()); } servers[server.sitename].add(server.id); }
Comments
Post a Comment