c# - Adding values using LINQ to an object property with a Dictionary property -
i have dataset returns couple of contact information in string(phone, mobile, skype). created object dictionary property can put contact information in key value pair. problem is, assigning values of object using linq. hope can help. here code:
public class student { public student() { mothercontacts = new contactdetail(); fathercontacts = new contactdetail(); } public contactdetail mothercontacts { get; set; } public contactdetail fathercontacts { get; set; } } public class contactdetail { public contactdetail() { items = new dictionary<contactdetailtype, string>(); } public idictionary<contactdetailtype, string> items { get; set; } public void add(contactdetailtype type, string value) { if(!string.isnullorempty(value)) { items.add(type, value); } } } public enum contactdetailtype { phone, mobile } here's how assign value student object:
var result = ds.tables[0].asenumerable(); var insurancecard = result.select(row => new student() { mothercontacts.items.add(contactdetailtype.phone, row.field<string>("motherphone"), mothercontacts.items.add(contactdetailtype.mobile, row.field<string>("mothermobile") }).firstordefault(); the compiler says mothercontacts not recognized in context. should do?
i think code should like:
var insurancecard = result.select(row => { var s = new student(); s.mothercontacts.items.add(contactdetailtype.phone, row.field<string>("motherphone"); s.mothercontacts.items.add(contactdetailtype.mobile, row.field<string>("mothermobile"); return s; }).firstordefault(); you using object initializer syntax in wrong way. correct use is:
new student{mothercontacts = value} value must contactdetail.
Comments
Post a Comment