Linq两级过滤器 [英] Linq two level filter

查看:78
本文介绍了Linq两级过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Linq使用两级过滤器。



粘贴是



1)xml摘录和

2)具有两个数据对象的代码片段,LineItem和LineList以及查询。



firstQuery的目标是创建一个List<> LineList对象,每个对象具有包含一个或多个LineItem对象的子列表。



我想首先过滤部分(section)级别属性(lvl)以缩小我的搜索范围。其次,我想根据Element(subSec)选择一个子部分(subSec)。属性(ssn)。



连接的'where'语句显示了我的过滤器的意图。



它只测试第一次出现的lvl和ssn,如果第一个不匹配,则不会深入到该部分来测试ssn的下一个值。



我已经尝试了各种方法来添加额外级别的'from'和'where'子句但是我不能完全解决顺序和语法问题它工作。我很感激任何建设性的意见或提示,让两级Linq查询工作。



谢谢



I have not been able to get a two-level filter to work using Linq.

Pasted up are

1) xml excerpt and
2) code fragment with two data objects, LineItem and LineList and a query.

The objective of firstQuery is to create a List<> of LineList objects each of which has a sublist comprising one or more LineItem objects.

I want to begin by filtering for the section (section) level Attribute (lvl) to narrow my search. Secondly, I want to select a subsection (subSec) based on the Element("subSec").Attribute ("ssn").

The concatenated ‘where’ statement shows the intent of my filter.

It only tests the first occurrence of lvl and ssn and does not drill down into the section to test the next value of ssn if the first one doesn’t match.

I have tried various ways of adding in an additional level of ‘from’ and ‘where’ clauses but I can’t quite work out the order and syntax to get it to work. I’d appreciate any constructive comments or hints to get a two-level Linq query to work.

Thanks

<mainxml>
<section lvl="0">
	<subsec ssn="a">
		<item>with</item>
		<item>a little</item>	
		<item>help</item>	
	</subsec>
	<subsec ssn="b">
		<item>from</item>
		<item>my</item>	
		<item>friends</item>
	</subsec>	
</section>
<section lvl="1">
	<subsec ssn="y">
		<item>Ive</item>
		<item>got</item>	
		<item>the</item>
	</subsec>		
	<subsec ssn="z">
		<item>music</item>
		<item>in</item>	
		<item>me</item>
	</subsec>
</section>
</mainxml>





我尝试过:





What I have tried:

//===============================================================
public class LineItem() //----------------------------------
{
	public int WdCtr {get; set;}
	public string Word {get; set;}
} // eo class LineItem -------------------------------------

public class LineList() // ---------------------------------
{
	public List<lineitem> Includes {get; set;}
} // eo class lineList -------------------------------------

void firstQuery() // ------------------------------------------
{
	int ctr = -1;

	var mainList = (from sect in XLineDoc.Descendents("section")
		where sect.Attirbute("lvl").Value=="1" && sect.Element("subSec").Attribute("ssn").Value ="z"			
                  select new LineList
                       {
                          Includes=(from wd in sect.Elements("subSec").Element("item") 
                                select new LineItem
				{
				 WdCtr=ctr++,
				 Word = wd.Value
				}).ToList()        
					                                          
			}).ToList();

} // eo firstQuery ----------------------------------------------</lineitem>

推荐答案

假设你只想要匹配 ssn 的小节,这样的东西应该有效:

Assuming you only want the subsections with a matching ssn, something like this should work:
from sect in XLineDoc.Descendants("section")
where (int?)sect.Attribute("lvl") == 1
let list = new LineList
{
    Includes = (from ss in sect.Elements("subSec")
                where (string)ss.Attribute("ssn") == "z"
                from wd in ss.Elements("item")
                select new LineItem
                {
                    WdCtr = ctr++,
                    Word = wd.Value
                }).ToList()
}
where list.Includes.Count != 0
select list;


这篇关于Linq两级过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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