How can I instantiate and add to a class inside a foreach loop in C# -
i have code want use log error messages. made 2 classes hold messages:
public class httpstatuserrors { public httpstatuserrors() { this.details = new list<httpstatuserrordetails>(); } public string header { set; get; } public ilist<httpstatuserrordetails> details { set; get; } } public class httpstatuserrordetails { public httpstatuserrordetails() { this.details = new list<string>(); } public string header { set; get; } public ilist<string> details { set; get; } }
i think way not 100% sure , welcome suggestions.
what in first class store header
in example "validation messages". in detail
object store header
showed entity error , in details
store validation errors.
what confused on how should instantiate classes. have done far. think correct. how can add details need add:
catch (dbentityvalidationexception ex) { var msg = new httpstatuserrors(); msg.header = "validation error"; foreach (var eve in ex.entityvalidationerrors) { var header = string.format("entity of type \"{0}\" in state \"{1}\" has following validation errors:", eve.entry.entity.gettype().name, eve.entry.state); foreach (var ve in eve.validationerrors) { var detail = string.format("- property: \"{0}\", error: \"{1}\"", ve.propertyname, ve.errormessage); } } }.
instantiate collections in details
properties. instantiate new items, , add them collections in loop:
var msg = new httpstatuserrors(); msg.header = "validation error"; msg.details = new list<httpstatuserrordetails>(); foreach (var eve in ex.entityvalidationerrors) { var detail = new httpstatuserrordetails() detail.header = string.format("entity of type \"{0}\" in state \"{1}\" has following validation errors:", eve.entry.entity.gettype().name, eve.entry.state); detail.details = new list<string>(); foreach (var ve in eve.validationerrors) { detail.details.add(string.format("- property: \"{0}\", error: \"{1}\"", ve.propertyname, ve.errormessage)); } msg.details.add(detail); }
btw can use linq that:
var msg = new httpstatuserrors(); msg.header = "validation error"; msg.details = ex.entityvalidationerrors .select(eve => new httpstatuserrordetails { header = string.format("entity of type \"{0}\" in state \"{1}\" has following validation errors:", eve.entry.entity.gettype().name, eve.entry.state) details = eve.validationerrors .select(ve => string.format("- property: \"{0}\", error: \"{1}\"", ve.propertyname, ve.errormessage)) .tolist() }).tolist();
also extension methods syntax:
public static httpstatuserrors tohttpstatuserrors( dbentityvalidationexception ex) { return new httpstatuserrors { header = "validation error", details = ex.entityvalidationerrors .select(eve => eve.tohttpstatuserrordetails()) .tolist() }; } public static httpstatuserrordetails tohttpstatuserrordetails( dbentityvalidationresult eve) { return new httpstatuserrordetails { header = string.format("entity of type \"{0}\" in state \"{1}\" has following validation errors:", eve.entry.entity.gettype().name, eve.entry.state), details = eve.validationerrors .select(ve => ve.toerrordescription()) .tolist() }; } public static string toerrordescription( dbvalidationerror ve) { return string.format("- property: \"{0}\", error: \"{1}\"", ve.propertyname, ve.errormessage); }
usage of such methods like:
catch (dbentityvalidationexception ex) { var msg = ex.tohttpstatuserrors(); }
Comments
Post a Comment