c# - check value exists in linq -
this xml file
<persons> <person> <id>1</id> <name>ingrid</name> </person> <person> <id>2</id> <name>ella</name> </person> </persons>
i using linq xml.
here id should auto-generated..
i need check if value of node id exists .
if not exists should create new id..how using linq. pointers?
thank you
xdocument doc = xdocument.parse(xml); int id = 1; // if need element xelement ingrid = (from person in doc.root.elements("person") (int)person.element("id") == id select person).firstordefault(); // if need know if there bool exists = (from person in doc.root.elements("person") (int)person.element("id") == id select person).any(); // generate new id int newid = ((from person in doc.root.elements("person") select (int?)person.element("id")).max() ?? 0) + 1;
Comments
Post a Comment