什么是做一个LINQ到XML语句动态的最佳途径? [英] What is the best way to make a LINQ-to-XML statement dynamic?

查看:106
本文介绍了什么是做一个LINQ到XML语句动态的最佳途径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载XML数据与此LINQ语句的对象:

  VAR smartFormFields =从smartFormField在xmlDoc.Descendants (smartFormField)
选择新Models.SmartFormField
{
IDCODE = smartFormField.Element(IDCODE)。价值
标签= smartFormField.Element(标签) .value的,
的FieldType = smartFormField.Element(字段类型)。价值
DisplayStatus = smartFormField.Element(displayStatus)。价值
requiredStatus = smartFormField.Element(requiredStatus ).value的,
DisplayColumn =(INT)smartFormField.Element(displayColumn),
DisplayOrder =(INT)smartFormField.Element(displayOrder),
说明= smartFormField.Element( 说明)。价值
示例= smartFormField.Element(榜样)。价值
controlType = smartFormField.Element(controlType)。价值
AutoSuggestDataSource = smartFormField.Element (autoSuggestDataSource)值
}。



但是,我的WHERE(和ORDERBY)语句每次都会改变,如:



这可能是这样的:

  VAR smartFormFields =从smartFormField在xmlDoc.Descendants(田)
其中smartFormField.Element(IDCODE)。价值==姓氏
选择新Models.SmartFormField
{...

这可能是这样的:

  VAR smartFormFields从smartFormField =在xmlDoc.Descendants(田)
,其中(INT)smartFormField.Element(DisplayOrder)值> 50
选择新Models.SmartFormField
{...



等。



我怎样才能把Where语句到一个变量,像这样:



伪代码:

 字符串whereStatement =其中(INT)smartFormField.Element(\DisplayOrder\) .value的大于50; 

变种smartFormFields从smartFormField =在xmlDoc.Descendants(田)
和;&安培; whereStatement
选择新Models.SmartFormField
{...


解决方案

你有来表达它作为一个字符串?如果你乐意提供它作为一个代表,你可以使用:

  //就像一个where子句的例子
Func键<的XElement,布尔> whereClause = SFF => (INT)sff.Element(DisplayOrder)值> 50;

VAR smartFormFields = xmlDoc.Descendants(田)
。凡(whereClause)
。选择(SFF =>
新Models.SmartFormField
{
IDCODE = sff.Element(IDCODE)值,
...
});

如果你把这个变成一个方法,那么你只需要使用一个lambda表达式:

  VAR栏= GetFormFields(SFF =>(INT)sff.Element(DisplayOrder)值方式> 50); 





这会很容易让你对不同的呼叫提供不同的价值观,但你不能只是把表达式到一个文本文件,而无需多做一些工作。这里有什么是你真正的要求是什么?你可以有图筛选器名称来函数求<的XElement,布尔> ,然后读取在执行时过滤的名字呢?或者,你真的需要它的完全的在执行时控制,以任意方式?



(注意顺序是类似的,但可能略麻烦......让我们筛选排序第一。)


I'm loading XML data into an object with this LINQ statement:

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
                      };

However, my WHERE (and ORDERBY) statement will change each time, e.g.

it may be this:

var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
                      where smartFormField.Element("IdCode").Value == "lastName"
                      select new Models.SmartFormField
                      {...

it may be this:

var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
                      where (int)smartFormField.Element("DisplayOrder").Value > 50
                      select new Models.SmartFormField
                      {...

etc.

How can I put the Where statement into a variable, something like this:

PSEUDO-CODE:

string whereStatement = "where (int)smartFormField.Element(\"DisplayOrder\").Value > 50";

var smartFormFields = from smartFormField in xmlDoc.Descendants("field")
                      &&whereStatement
                      select new Models.SmartFormField
                      {...

解决方案

Do you have to express it as a string? If you're happy to supply it as a delegate, you can use:

// 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,
                                    ...
                                });

If you put this into a method, then you just need to use a lambda expression:

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

etc

That will let you easily supply different values for different calls, but you won't be able to just put the expressions into a text file without a bit more work. What are your real requirements here? Could you have a map of "filter name" to Func<XElement, bool>, and then read the filter names at execution time? Or do you really need it to be fully controlled at execution time, in an arbitrary way?

(Note that ordering is similar but probably slightly trickier... let's get the filtering sorted first.)

这篇关于什么是做一个LINQ到XML语句动态的最佳途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆