Is there an initialization syntax to the ExpandoObject
that I can use to my advantage in a LINQ to XML query for brevity purposes?
Note: The results of the query are intended to be passed outside the scope of the current assembly so anonymous types are out of the question (see why here).
I'm trying to use brief syntax like the following to create dynamic/expando objects:
public IEnumerable<dynamic> ParseUserXml(string strXmlUser) {
var qClients =
from client in xdoc.Root.Element(XKey.clients).Elements(XKey.client)
// client object
// I cannot get ExpandoObject to initialize inline
select new ExpandoObject() { // try initialization syntax, COMPILE ERR
OnlineDetails = new
{
Password = client.Element(XKey.onlineDetails).
Element(XKey.password).Value,
Roles = client.Element(XKey.onlineDetails).
Element(XKey.roles).Elements(XKey.roleId).
Select(xroleid => xroleid.Value)
// More online detail fields TBD
},
// etc ....
// YIELD DYNAMIC OBJECTS BACK THROUGH IEnumerable<dynamic>...
foreach (var client in qClients)
{
yield return client;
}
More involved solutions to make this code work might be:
Is there an equally short syntax to achieve what I intend to do by the erroneous code in question, or will I have to expand the code base in some manner to get the desired result?
Unfortunately I don't think you'll be able to do this. What you can do is create a helper method and call it.
var qClients =
from client in xdoc.Root.Element(XKey.clients).Elements(XKey.client)
// client object
select ClientXmlToExpandoObject(client);
The helper might look something like
public dynamic ClientXmlToExpandoObject(System.Xml.Linq.XElement client)
{
dynamic result = new ExpandoObject();
result.OnlineDetails = new
{
Password = client.Element(XKey.onlineDetails).
Element(XKey.password).Value,
Roles = client.Element(XKey.onlineDetails).
Element(XKey.roles).Elements(XKey.roleId).
Select(xroleid => xroleid.Value)
// More online detail fields TBD
};
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With