简体   繁体   English

LINQ to XML的条款

[英]WHERE IN CLAUSE for LINQ to XML

The following code should return all barcodes that are in the filter. 以下代码应返回过滤器中的所有条形码。 however it seems to excecute the filter only on the first newrecord. 但是,它似乎只在第一个新记录上执行过滤器。

the xml contains 5 newrecords with a barcode subelement ranging 1-5. xml包含5个newrecord,其中条码子元素的范围为1-5。 if the filter contains 1,3 and 4 it returns 1 and if the filter contains 2,3 and 4 it returns a no records. 如果过滤器包含1,3和4,则返回1;如果过滤器包含2,3和4,则返回no。

Also the current setup (rpa.XMLData.Elements("NEWFILE").Elements("NEWRECORD") ) returns 5 rows. 同样,当前设置(rpa.XMLData.Elements(“ NEWFILE”)。Elements(“ NEWRECORD”))返回5行。 i have tried seeing what happens if i change this to comeback in a single row (rpa.XMLData.Elements("NEWFILE")) but this does the same in giving me back 1 result being the first hit, ignoring 3 and 4. 我尝试查看如果我将其更改为单行返回(rpa.XMLData.Elements(“ NEWFILE”))会发生什么,但这在给我返回1结果作为第一个命中结果时忽略了3和4,效果相同。

is there a way to change this LINQ Statement to actually look through all the barcodes? 有没有办法更改此LINQ语句以实际查看所有条形码?

edit: the information comes from a test atm so sorry if it looks a bit weird. 编辑:信息来自一个测试atm,所以很抱歉,如果它看起来有点奇怪。 i added info on the xml and the filter. 我在xml和过滤器上添加了信息。

also, i have found something extra on the problem if the 此外,我发现了一些额外的问题,如果

filter contains 1,2,3 it works. 过滤器包含1,2,3起作用。 filter contains 1,2,4 it will return 1 and 2 not 4. filter包含1,2,4,它将返回1和2而不是4。

it seems to be breaking once it can't find a solution. 一旦找不到解决方案,它似乎就崩溃了。

String string2Stream = String.Concat("2", Environment.NewLine, "3", Environment.NewLine, "4", Environment.NewLine, "End");
        Stream reader = new MemoryStream(ASCIIEncoding.Default.GetBytes(string2Stream));
   StreamReader read = new StreamReader( reader );
   var filter = Enumerable.Where(StreamReaderToSeq(read), x => { int temp; return int.TryParse(x, out temp); });


   var query = from p in rpa.XMLData.Elements("NEWFILE").Elements("NEWRECORD") 
               where filter.Contains(p.Element("BAR_CODE").Value)
               select new { p.Element("BAR_CODE").Value };   

Underneath the xml i deleted alot of extra stuff that shouldn't be related. 在xml下方,我删除了许多不相关的额外内容。

<?xml version=\"1.0\"?>
<NEWFILE>

<NEWRECORD num=\"1\"><MAILSORT></MAILSORT><BAR_CODE>1</BAR_CODE>
 </NEWRECORD> 

<NEWRECORD num=\"2\"><MAILSORT></MAILSORT><BAR_CODE>2</BAR_CODE>  
</NEWRECORD>

<NEWRECORD num=\"3\"><MAILSORT></MAILSORT><BAR_CODE>3</BAR_CODE>  
</NEWRECORD>

<NEWRECORD num=\"4\"><MAILSORT></MAILSORT><BAR_CODE>4</BAR_CODE>  
</NEWRECORD>

<NEWRECORD num=\"5\"><MAILSORT></MAILSORT><BAR_CODE>5</BAR_CODE>  
</NEWRECORD>

</NEWFILE>
var filter = new List<string> {"1", "2", "4"};

var query = from p in barcode.Descendants("BAR_CODE")
            where filter.Contains(p.Value)
            select p.Value;

I wasn't sure where you were going with the filter you created but I think this is a pretty safe query. 我不确定创建的过滤器要去哪里,但是我认为这是一个非常安全的查询。 It's handy because it works even if BAR_CODE is empty. 这很方便,因为即使BAR_CODE为空也可以使用。

As an alternative answer, This shows how to make it work without contains but keeping the stream. 作为替代答案, 显示了如何使其在不包含但保持流的情况下工作。

converted to my code it works too. 转换为我的代码也可以。

BUT why does the Contains seem to break if one fails while it works ok if you replace the stream with a List or if you replace the Contains with a JOIN. 但是,如果用List替换流或用JOIN替换Contains时,如果包含失败却无法正常工作,为什么Contains似乎会中断。 anyone kniow the answer to that? 有人知道答案吗?

      var query = from p in rpa.XMLData.Elements("NEWFILE").Elements("NEWRECORD") 
                join f in filter on
                    p.Element("BAR_CODE").Value equals f.ToString()                    
                select p; 

or 要么

       var projectsMemberWorkedOn =
                    rpa.XMLData.Elements("NEWFILE").Elements("NEWRECORD")
                    .Join(filter, p => p.Element("BAR_CODE").Value, f => f.ToString(), 
                        ( p, f ) => new { p, f } ) 
                        .Select(@t => @t.p ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM