【问题标题】:What is the best way to make a LINQ-to-XML statement dynamic?使 LINQ-to-XML 语句动态化的最佳方法是什么?
【发布时间】: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


    【解决方案1】:

    您必须将其表示为字符串吗?如果您愿意作为代表提供它,您可以使用:

    // Just as one example of a where clause
    Func<XElement, bool> whereClause = sff => (int) sff.Element("DisplayOrder").Value > 50;
    
    var smartFormFields = xmlDoc.Descendants("field")
                                .Where(whereClause)
                                .Select(sff => 
                                    new Models.SmartFormField
                                    {
                                        IdCode = sff.Element("idCode").Value,
                                        ...
                                    });
    

    如果你把它放到一个方法中,那么你只需要使用一个 lambda 表达式:

    var fields = GetFormFields(sff => (int) sff.Element("DisplayOrder").Value > 50);
    

    这将使您可以轻松地为不同的调用提供不同的值,但是您将无法仅将表达式放入文本文件中而无需做更多的工作。您在这里的真正要求是什么?您能否将“过滤器名称”映射到Func&lt;XElement, bool&gt;,然后在执行时读取过滤器名称?或者您真的需要在执行时以任意方式完全控制它吗?

    (请注意,排序是相似的,但可能有点棘手……让我们先对过滤进行排序。)

    【讨论】:

    • 很有意思,要求是:能够从构造函数解析各种“加载代码”从XML文件中加载各种组,所以我只需要一个将文本转换为委托的switch语句,让build 尝试在...中构建它。
    • 我只需要一个 Dictionary> 并使用它而不是 switch 语句。
    • 虽然这不是我的帖子,但我仍然希望您发布一个完整的在 LINQ 中使用字符串的小型工作示例,例如 xmlDoc.Descendants("field") .Where(whereClause)
    • @Thomas:你还需要什么? ... 只是一个 lambda 表达式。
    • where delegate function code 未完成。我需要看看委托代码的样子……因为我不熟悉那种 where 子句。谢谢
    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多