c# - How to create a Dictionary using foreach statement -
i have model tasks, in each task had write down name of client, have model clients contains id, name, adress , phone number. can create customer manually when create task, supposed have dropdownlist contains every customer created
it's pretty hard me explain you... know have create dictionnary , add customers in dictionnary foreach. can any1 me showing me examples? if need more details let me know
appreciate help
using system; using system.collections.generic; using system.componentmodel; using system.componentmodel.dataannotations; using system.configuration; using system.data; using system.data.sqlclient; using system.linq; using system.security.cryptography; using system.text; using system.web; namespace taskmanager.models { public class client { public int id { get; set; } public string nom { get; set; } public string adresse { get; set; } public int numerotelephone { get; set; } public static list<client> getlist() { string cstr = configurationmanager.connectionstrings["listtask"].connectionstring; using (sqlconnection cnx = new sqlconnection(cstr)) { string requete = "select * tableclient"; sqlcommand cmd = new sqlcommand(requete, cnx); cmd.commandtype = system.data.commandtype.text; try { cnx.open(); sqldatareader datareader = cmd.executereader(); list<client> clientlist = new list<client>(); while (datareader.read()) { client t = new client(); t.nom = (string)datareader["nom"]; t.adresse = (string)datareader["adresse"]; t.numerotelephone = (int)datareader["numerotelephone"]; clientlist.add(t); } datareader.close(); return clientlist; } { cnx.close(); } } } public static client findone(int id) { string cstr = configurationmanager.connectionstrings["mycnx"].connectionstring; using (sqlconnection cnx = new sqlconnection(cstr)) { string requete = "select * tableclient id = " + id; sqlcommand cmd = new sqlcommand(requete, cnx); cmd.commandtype = system.data.commandtype.text; try { cnx.open(); sqldatareader datareader = cmd.executereader(); client t = new client(); while (datareader.read()) { t.id = (int)datareader["id"]; t.nom = (string)datareader["nom"]; t.adresse = (string)datareader["adresse"]; t.numerotelephone = (int)datareader["numerotelephone"]; } datareader.close(); return t; } { cnx.close(); } } } public bool saveasnew() { string cn = configurationmanager.connectionstrings["mycnx"].connectionstring; using (sqlconnection cnx = new sqlconnection(cn)) { // utilisation de la connexion string requete = "insert tableclient (nom, adresse, numerotelephone)"; requete += "values (@nom, @adresse, @numerotelephone)"; sqlcommand cmd = new sqlcommand(requete, cnx); cmd.commandtype = system.data.commandtype.text; //définir les paramètres cmd.parameters.add("nom", sqldbtype.nvarchar); cmd.parameters.add("adresse", sqldbtype.nvarchar); cmd.parameters.add("numerotelephone", sqldbtype.int); //donner des valeurs aux paramètres cmd.parameters["nom"].sqlvalue = this.nom; cmd.parameters["adresse"].sqlvalue = this.adresse; cmd.parameters["numerotelephone"].sqlvalue = this.numerotelephone; cnx.open(); cmd.executenonquery(); cnx.close(); return true; } } public bool update() { string cn = configurationmanager.connectionstrings["mycnx"].connectionstring; using (sqlconnection cnx = new sqlconnection(cn)) { // utilisation de la connexion string requete = "update task set "; requete += "nom=@nom,"; requete += "adresse=@adresse,"; requete += "numero de telephone=@numerotelephone,"; requete += "where id = " + this.id; sqlcommand cmd = new sqlcommand(requete, cnx); cmd.commandtype = system.data.commandtype.text; //définir les paramètres cmd.parameters.add("nom", sqldbtype.nvarchar); cmd.parameters.add("adresse", sqldbtype.nvarchar); cmd.parameters.add("numerotelephone", sqldbtype.int); //donner des valeurs aux paramètres cmd.parameters["nom"].sqlvalue = this.nom; cmd.parameters["adresse"].sqlvalue = this.adresse; cmd.parameters["numerotelephone"].sqlvalue = this.numerotelephone; cnx.open(); cmd.executenonquery(); cnx.close(); return true; } } public static bool destroy(int id) { string cn = configurationmanager.connectionstrings["mycnx"].connectionstring; using (sqlconnection cnx = new sqlconnection(cn)) { string requete = "delete tableclient id = " + id; sqlcommand cmd = new sqlcommand(requete, cnx); cmd.commandtype = system.data.commandtype.text; cnx.open(); cmd.executenonquery(); cnx.close(); return true; } }
this not working
public static readonly dictionary<int, string> clients = new dictionary<int, string>() { foreach c in client.getlist() {clients.add(id, "test" } } }
here example of same thing working, have manually add each item dictionnary...basically first dictionnary supposed go on model client created , every client in , add them dictionnary...hope not making difficult you.
public static readonly dictionary<int, string> priorities = new dictionary<int, string> { { 0, "choisir une valeur!" }, { 1, "1 - pas urgent pantoute!" }, { 2, "2 - bah! ça serait le fun que ça soit fait un jour!" }, { 3, "3 - pour hier!" } };
you don't want declare clients
dictionary readonly
, unless you're going create in static constructor. if that's want do, have create static constructor:
// static dictionary of clients public static readonly dictionary<int, string> clients; // static constructor client class static client() { // create , populate clients dictionary clients = new dictionary<int, string>(); foreach (var c in getlist()) { clients.add(c.id, c.nom); } }
if that, though, you'll have add new items dictionary whenever create new record, , update dictionary if change name. remember, static constructor run one time--when client
type first accessed.
you can shorten code in client
constructor it's single linq statement:
clients = getlist().todictionary(c => c.id, c => c.nom);
it looks want, though, build dictionary on demand. in case, have getclientsdictionary
method can call when need it:
public static dictionary<int, string> getclientsdictionary() { return getlist().todictionary(c => c.id, c => c.nom); }
Comments
Post a Comment