嵌套 Linq 分组依据
我所拥有的:列表
类别:规则 --> 班级:学期 ---> 属性比较_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这样的事情:
我在这里所做的(无论如何尝试过,没有检查这是否真的适用于您的模型)是反转查询。首先获取所有术语,按多个字段键将它们分组,然后为每个组选择相关规则。
Try something like this:
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.
为什么单个组还不够?您可以使用匿名类按多个术语进行分组:
这将为您提供一个
IGrouping
,尽管您可以将其转换为列表列表,但您可以使用SelectMany()
展平此结构>:Why doesn't a single group by suffice? You can group by multiple terms using an anonymous class:
This would give you an
IGrouping
though that you can convert to a list of lists, you could flatten this structure usingSelectMany()
:不要编写只写代码。我知道使用 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.
你可以尝试
You might try
很难确切地知道你想要什么,因为这个例子不太清楚。你看过这两个资源吗? MSDN 和 101 个 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.