嵌套 Linq 分组依据

发布于 2024-11-15 09:04:05 字数 1239 浏览 1 评论 0原文

我所拥有的:列表

类别:规则 --> 班级:学期 ---> 属性比较_Src 属性 Compare_Mode

我想要做什么: 使用 Linq 语句获取我的规则,按 Compare_Src 和 Compare_Mode 的组合进行分组。

一种组合的静态示例:(这里我得到了所有规则的列表,其中 Compare_src = EmpfaengerToken 和 Compare_mode = equals。我想要的是包含所有规则的完整列表,按两个属性分组,而不是硬编码值。

            var regeln_empfaengertoken  = 
        (
            from rule in rules 
            where 
            (
                from term in rule.Term 
                    where 
                    (
                        term.Compare_src == Entities.Enums.Compare_src.EmpfaengerToken
                        &&
                        term.Compare_mode.ToString() == "equals"
                    )
                    select term
            ).Count() > 0 
            select rule
        ).ToList<Entities.Rule>();

我知道这是可能的,但找不到正确的解决方案:(

  [Serializable]
public class Rule
{
    private List<Term> _term = new List<Term>();
    ...
}


   [Serializable]
public class Term
{
    private Entities.Enums.Compare_src _compare_src;
    private Entities.Enums.Compare_mode _compare_mode;
    private Entities.Enums.Compare_Type _compare_type;
    .........
 }

What I have: List

Class: Rule
-->
Class: Term
--->
Property Compare_Src
Property Compare_Mode

What I want to do:
use a Linq statement to get my Rules, grouped by a Combination of its Compare_Src and Compare_Mode.

Static Example for one Combination: (Here I get a list for all rules with Compare_src = EmpfaengerToken and Compare_mode = equals. What I want is a full list with all rules, grouped by the two propertys, instead of hardcoded values.

            var regeln_empfaengertoken  = 
        (
            from rule in rules 
            where 
            (
                from term in rule.Term 
                    where 
                    (
                        term.Compare_src == Entities.Enums.Compare_src.EmpfaengerToken
                        &&
                        term.Compare_mode.ToString() == "equals"
                    )
                    select term
            ).Count() > 0 
            select rule
        ).ToList<Entities.Rule>();

I know it's possible, but can't find the right solution :(

  [Serializable]
public class Rule
{
    private List<Term> _term = new List<Term>();
    ...
}


   [Serializable]
public class Term
{
    private Entities.Enums.Compare_src _compare_src;
    private Entities.Enums.Compare_mode _compare_mode;
    private Entities.Enums.Compare_Type _compare_type;
    .........
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心是晴朗的。 2024-11-22 09:04:05

尝试这样的事情:

rules.SelectMany(rule => rule.Term)
     .GroupBy(term => new { term.Compare_src, term.Compare_mode })
     .Select(termGroup => 
           new {
              Key = termGroup.key, 
              Rules = rules.Where(r => r.Term.Any(t => 
                 t.Compare_src == termGroup.key.Compare_src && 
                 t.Compare_mode == termGroup.key.Compare_mode
              )
               }
             )

我在这里所做的(无论如何尝试过,没有检查这是否真的适用于您的模型)是反转查询。首先获取所有术语,按多个字段键将它们分组,然后为每个组选择相关规则。

Try something like this:

rules.SelectMany(rule => rule.Term)
     .GroupBy(term => new { term.Compare_src, term.Compare_mode })
     .Select(termGroup => 
           new {
              Key = termGroup.key, 
              Rules = rules.Where(r => r.Term.Any(t => 
                 t.Compare_src == termGroup.key.Compare_src && 
                 t.Compare_mode == termGroup.key.Compare_mode
              )
               }
             )

What I did here (tried anyway, didn't check if this actually works with your model) is to inverse the query. First get all the terms, group them by the mutiple field key and then select the relevant rules for each group.

如若梦似彩虹 2024-11-22 09:04:05

为什么单个组还不够?您可以使用匿名类按多个术语进行分组:

var ruleGroups = from rule in rules 
                 group by new { rule.Term.Compare_src, rule.Term.Compare_mode } 
                 into g
                 select ...;

这将为您提供一个 IGrouping,尽管您可以将其转换为列表列表,但您可以使用 SelectMany() 展平此结构>:

var orderedRuleList = ruleGroups.SelectMany( x => x)
                                .ToList();

Why doesn't a single group by suffice? You can group by multiple terms using an anonymous class:

var ruleGroups = from rule in rules 
                 group by new { rule.Term.Compare_src, rule.Term.Compare_mode } 
                 into g
                 select ...;

This would give you an IGrouping though that you can convert to a list of lists, you could flatten this structure using SelectMany():

var orderedRuleList = ruleGroups.SelectMany( x => x)
                                .ToList();
梦魇绽荼蘼 2024-11-22 09:04:05

不要编写只写代码。我知道使用 lambda 和扩展方法的语法糖很诱人,但我建议将中间结果分配给临时持有者。

Don't write write-only code. I know it is tempting to go off on this syntactic sugar for lambdas and extension methods, but I'd advice to assign intermediate results to temporary holders.

零時差 2024-11-22 09:04:05

你可以尝试

group term by new { term.Compare_src, term.Compare_mode }

You might try

group term by new { term.Compare_src, term.Compare_mode }
丑疤怪 2024-11-22 09:04:05

很难确切地知道你想要什么,因为这个例子不太清楚。你看过这两个资源吗? MSDN101 个 Linq 示例

如果您的问题更准确,那么会更容易帮助您。

It's hard to know exactly what you want since the example is not quite so clear. Have you looked at these two resources? MSDN and 101 Linq examples.

If you are more precise in your question it would be easier to help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文