【发布时间】:2023-03-25 07:03:01
【问题描述】:
我正在使用此 LINQ 语句将 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
};
但是,我的 WHERE(和 ORDERBY)语句每次都会改变,例如
可能是这样的:
var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
where smartFormField.Element("IdCode").Value == "lastName"
select new Models.SmartFormField
{...
可能是这样的:
var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
where (int)smartFormField.Element("DisplayOrder").Value > 50
select new Models.SmartFormField
{...
等等。
如何将 Where 语句放入变量中,如下所示:
伪代码:
string whereStatement = "where (int)smartFormField.Element(\"DisplayOrder\").Value > 50";
var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
&&whereStatement
select new Models.SmartFormField
{...
【问题讨论】:
标签: c# linq linq-to-xml