c# - Modify only part of object properties -
i have class structure this:
[xmlroot(elementname = "details")] public class details { [xmlelement(elementname = "size")] public string size { get; set; } [xmlelement(elementname = "length")] public string length { get; set; } } [xmlroot(elementname = "notes")] public class notes { [xmlelement(elementname = "text")] public string text { get; set; } } [xmlroot(elementname = "data")] public class data { [xmlelement(elementname = "id")] public string id { get; set; } [xmlelement(elementname = "name")] public string name { get; set; } [xmlelement(elementname = "filename")] public string filename { get; set; } [xmlelement(elementname = "status")] public string status { get; set; } [xmlelement(elementname = "date")] public string date { get; set; } [xmlelement(elementname = "details")] public details details { get; set; } [xmlelement(elementname = "notes")] public notes notes { get; set; } } [xmlroot(elementname = "alldata")] public class alldata { [xmlelement(elementname = "data")] public data[] data { get; set; } }
i have requirement in update part of class eg. update data(name, filename, status, details etc.) id=1 , write whole alldata properties xml file.
i reading data xml file using xml serializer , displaying in text boxes user allowed modify data. once user modifies data has put same xml file.
i tried using linq not getting correctly.
this returning relevant data modify.
data data= new data(); data = (from data in alldata.data data.id == "1" select data).firstordefault();
now want modify data id=1: this:
var query = (from records in recordings.record records.id == "1" select records).select(x => { x.name = "foo"; return x; }).firstordefault();
linq means language integrated query. it's querying data, not manipulating it.
you selected data want manipulate:
var data = (from data in alldata.data data.id == "1" select data).tolist();
personally, prefer method syntax, it's same really:
var data = alldata.data.where(data => data.id == "1").tolist();
now have data, need manipulate it. in old fashioned way:
foreach(var d in data) { d.name = "foo"; }
now data changed. might want display changes or write them file.
Comments
Post a Comment