Je chargement des données XML dans un objet avec cette déclaration LINQ:Quelle est la meilleure façon de rendre dynamique une instruction LINQ-to-XML?
var smartFormFields = from smartFormField in xmlDoc.Descendants("smartFormField")
select new Models.SmartFormField
{
IdCode = smartFormField.Element("idCode").Value,
Label = smartFormField.Element("label").Value,
FieldType = smartFormField.Element("fieldType").Value,
DisplayStatus = smartFormField.Element("displayStatus").Value,
RequiredStatus = smartFormField.Element("requiredStatus").Value,
DisplayColumn = (int)smartFormField.Element("displayColumn"),
DisplayOrder = (int)smartFormField.Element("displayOrder"),
Description = smartFormField.Element("description").Value,
Example = smartFormField.Element("example").Value,
ControlType = smartFormField.Element("controlType").Value,
AutoSuggestDataSource = smartFormField.Element("autoSuggestDataSource").Value
};
Cependant, ma déclaration WHERE (et ORDERBY) change chaque fois que, par exemple
il peut être ceci:
var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
where smartFormField.Element("IdCode").Value == "lastName"
select new Models.SmartFormField
{...
il peut être ceci:
var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
where (int)smartFormField.Element("DisplayOrder").Value > 50
select new Models.SmartFormField
{...
etc.
Comment puis-je mettre la déclaration en cas d'une variable, quelque chose comme ça :
PSEUDO-C ODE:
string whereStatement = "where (int)smartFormField.Element(\"DisplayOrder\").Value > 50";
var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
&&whereStatement
select new Models.SmartFormField
{...
très intéressant, les exigences sont les suivantes: être en mesure d'analyser différents « codes de charge » du constructeur pour charger divers groupes à partir du fichier XML, donc je vais juste avoir une instruction switch qui convertit le texte en délégués, laissez construire essayer de construire cela dans ... –
Je voudrais juste un Dictionary
bien que ce ne soit pas mon article mais j'aime quand même vous demander de publier un petit exemple complet d'utilisation de chaînes dans LINQ comme 'xmlDoc.Descendants (" field ") .Where (whereClause)' – Thomas