Lambda迭代过滤 [英] Lambda Iterational Filtering

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

问题描述

亲爱的,



我在C#中使用lambda表达式,以便在数据库实体中进行过滤。我正在访问的表使用分层树结构,并且特定实体可以具有无限级别的子级,可以通过.Children访问,例如:



< pre lang =c#> string name = entity.Children.First()。Children.First()。Children.First()。Name;





我正在尝试查找包含给定孩子特定值的根节点。因此,例如,该方法将采用 name 参数,该参数可以是Level1.Level2.Name value 参数,可以是测试



所以在这种情况下,我需要检查一个名为 Level1 的子节点,这个孩子必须包含一个名为 Level2 的孩子,它有另一个名为 Name 的孩子,其值为 Test





我到目前为止尝试的是这样的:



  var 谓词= PredicateBuilder.True< Attribute>(); 
predicate = predicate.And(a = > a.ParentId == 5 )。扩大();

// segments是由'。'分割成字符串[]的输入名称。字符
for int x = 0 ; x < segmentss.Length; x ++)
{
string tmpSegment = segments [x];

predicate = predicate.And(e = > e.Children.Any(c = > c.Name.Equals(tmpSegment,StringComparison.OrdinalIgnoreCase)))。Expand();
}

query = query.Where(predicate);

// 执行查询并检索结果......





问题是这个查询都是在基节点上进行评估的。我需要对其子女和孩子的孩子重复名称检查谓词。



如果我的问题的任何部分不清楚,请不要犹豫。



谢谢!





编辑#1:



LinqKit已经在这个项目的其他部分使用。所以如果你有一个使用LinqKit的建议,也可以接受:)

解决方案

我终于通过从叶子开始解决问题了使用查询联接的顺序,例如:



 query = query.Join(子查询,c = >  c.ParentId,p = >  p.Id,(c,p)=  >  p); 





无论如何,谢谢你。


Dear all,

I am using lambda expressions in C# in order to filter within a database entity. The table I am accessing is using a hierarchical tree structure and a particular entity can have infinite levels of children, which can be accessed by .Children, for example:

string name = entity.Children.First().Children.First().Children.First().Name;



I am trying to find a root node that contains a particular value for a given child. So for instance, the method will take a name parameter which can be "Level1.Level2.Name" and value parameter which can be "Test".

So in this case, I need to check for a node which has a Child named Level1, and this child must in turn contain a Child named Level2 which has another Child called Name and has the value of Test.


What I tried so far is something like this:

var predicate = PredicateBuilder.True<Attribute>();
predicate = predicate.And(a => a.ParentId == 5).Expand();

// segments is the input name split into a string[] by the '.' character
for (int x = 0; x < segments.Length; x++)
{
    string tmpSegment = segments[x];

    predicate = predicate.And(e => e.Children.Any(c => c.Name.Equals(tmpSegment, StringComparison.OrdinalIgnoreCase))).Expand();
}

query = query.Where(predicate);

// Execute query and retrieve results...



The problem is that this query is all being evaluated on the base node. I need to repeat the Name checking predicate on its Children, and the Children's Children.

If any part of my question is unclear do not hesitate to ask.

Thanks!


Edit #1:

LinqKit is already being used in other parts of this project. So if you have a suggestion that uses LinqKit, it is also acceptable :)

解决方案

I have finally solved by starting from the leaf and work myself up to the parents in order using query joins, such as:

query = query.Join(subquery, c => c.ParentId, p => p.Id, (c, p) => p);



Thank you anyway.


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

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