Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xlinq查询返回null WhereEnumerableIterator<&燃气轮机;_C#_Linq_Linq To Xml_Xelement - Fatal编程技术网

C# Xlinq查询返回null WhereEnumerableIterator<&燃气轮机;

C# Xlinq查询返回null WhereEnumerableIterator<&燃气轮机;,c#,linq,linq-to-xml,xelement,C#,Linq,Linq To Xml,Xelement,我试图在c#4.0中执行过滤xlinq查询,以便绑定到DataContext。代码如下: public IEnumerable<XElement> Filter(int min = 0, int max = int.MaxValue) { IEnumerable<XElement> selected = ( from x in xmlFile.Elements("Product") where (int)x.Element("Pri

我试图在c#4.0中执行过滤xlinq查询,以便绑定到DataContext。代码如下:

public IEnumerable<XElement> Filter(int min = 0, int max = int.MaxValue)
{
    IEnumerable<XElement> selected = (
        from x in xmlFile.Elements("Product")
        where (int)x.Element("Price") >= min &&
              (int)x.Element("Price") <= max
        select x);

    return selected;
}
minprice和maxprice是文本框,KeyUp事件从上面触发函数


编辑2

我想出来了,看看我的答案。但我无法解释为什么会这样,有人能帮我理解吗

提前谢谢

如果你有“空”或“不存在”的价格要素,它将被打破

试试这个:

public static IEnumerable<XElement> Filter(int min = 0, int max = int.MaxValue)
{
    Func<XElement, int?> parse = p => {
        var element = p.Element("Price");

        if (element == null) {
            return null;
        }

        int value;

        if (!Int32.TryParse(element.Value, out value)) {
            return null;
        }

        return value;
    };

    IEnumerable<XElement> selected = (
        from x in xmlFile.Elements("Product")
        let value = parse(x)
        where value >= min &&
            value <= max
        select x);

    return arr;
}
公共静态IEnumerable筛选器(int min=0,int max=int.MaxValue)
{
Func parse=p=>{
var要素=p要素(“价格”);
if(元素==null){
返回null;
}
int值;
如果(!Int32.TryParse(element.Value,out Value)){
返回null;
}
返回值;
};
选定的IEnumerable=(
从xmlFile.Elements(“产品”)中的x开始
让value=parse(x)
其中值>=min&&

value我已经弄明白了!虽然我不知道为什么Xlinq方法不起作用……以下是对我有效的代码,而不是查询:

    public XElement filter(int min = 0, int max = int.MaxValue)
    {            
        XElement filtered = new XElement("Stock");
        foreach (XElement product in xmlFile.Elements("Product"))
        {
            if ((int)product.Element("Price") >= min &&
                (int)product.Element("Price") <= max)
                    filtered.Add(product);
        }
        return filtered;
    }
public-XElement过滤器(int-min=0,int-max=int.MaxValue)
{            
XElement过滤=新XElement(“股票”);
foreach(xmlFile.Elements(“产品”)中的XElement产品)
{
如果((int)product.Element(“价格”)>=min&&
(int)product.Element(“Price”)您有两个问题:

  • 当您将鼠标悬停在
    WhereEnumerableIterator
    上并看到
    。当前的
    null
    ,一切都正常工作。一些LINQ查询(这也适用于XLinq)在您枚举它们之前不要执行,因此
    。在您使用它之前,Current
    将为
    null
    。当您在回答中使用
    foreach
    时,它枚举迭代器并生成一个值

  • 您的初始代码不起作用,因为它返回了一个不带根元素的XML枚举,并且无论您的调用代码是什么,它都会出现,它要求它具有根。您的答案将数据包装在
    元素中。您可以像这样使用原始代码:

    public XElement Filter(int min = 0, int max = int.MaxValue)
    {
        var selected = (
            from x in xmlFile.Elements("Product")
            where (int)x.Element("Price") >= min &&
                  (int)x.Element("Price") <= max
            select x);
    
        return new XElement("Stock", selected);
    }
    
    public-XElement过滤器(int-min=0,int-max=int.MaxValue)
    {
    所选变量=(
    从xmlFile.Elements(“产品”)中的x开始
    其中(int)x.Element(“价格”)>=min&&
    
    (int)x.Element(“价格”)请给出一个简短但完整的例子来说明这个问题。我可以想象返回值是空的,但不是空的。特别是,如果它将类型显示为WhereEnumerableTerator,那么它显然不是空的!嘿,我编辑了我的评论,谢谢你的关注!@tinchou:
    Current
    null
    ,因为它不是枚举数ed yet.Hi,“元素(“股票”)是不对的,因为我在xmlFile中使用的是XElement,而不是XDocument。所以我不必放根,股票。我发现“元素(“产品”)”语句失败,它返回一个空System.Xml.Linq.XContainer。您有什么想法吗?谢谢您的响应!您在调试器中看到的是正常的,因为Linq查询尚未枚举。绑定失败,因为您没有将其包装到
    Stock
    XElement中。
        public XElement filter(int min = 0, int max = int.MaxValue)
        {            
            XElement filtered = new XElement("Stock");
            foreach (XElement product in xmlFile.Elements("Product"))
            {
                if ((int)product.Element("Price") >= min &&
                    (int)product.Element("Price") <= max)
                        filtered.Add(product);
            }
            return filtered;
        }
    
    public XElement Filter(int min = 0, int max = int.MaxValue)
    {
        var selected = (
            from x in xmlFile.Elements("Product")
            where (int)x.Element("Price") >= min &&
                  (int)x.Element("Price") <= max
            select x);
    
        return new XElement("Stock", selected);
    }