Linq 自连接和过滤
我有一个由此类组成的 List
:
public class ClaimEvent
{
public ClaimEventType ClaimEventClaimEventType { get; set; }
public DateTime OccurredOn { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
}
'ClaimEventType' 就像这样...
public class ClaimEventType
{
public ClaimEventType()
{
Cancels = new List<ClaimEventType>();
CancelledBy = new List<ClaimEventType>();
}
public int ClaimEventTypeId { get; set; }
public string ClaimEventTypeName { get; set; }
public List<ClaimEventType> Cancels { get; set; }
public List<ClaimEventType> CancelledBy { get; set; }
}
Cancels
列出了此事件出现在它们之后时取消的所有事件类型在按 OccurredOn
排序的列表中。 CancelledBy
是相反的,也就是说,如果 CancelledBy
事件之一出现在该事件之后,则该事件被取消。
如何查询这些对象的列表,以便被列表中其他项目取消的项目不会出现在结果中?
I have a List<ClaimEvent>
made up of this class:
public class ClaimEvent
{
public ClaimEventType ClaimEventClaimEventType { get; set; }
public DateTime OccurredOn { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
}
'ClaimEventType' is like so...
public class ClaimEventType
{
public ClaimEventType()
{
Cancels = new List<ClaimEventType>();
CancelledBy = new List<ClaimEventType>();
}
public int ClaimEventTypeId { get; set; }
public string ClaimEventTypeName { get; set; }
public List<ClaimEventType> Cancels { get; set; }
public List<ClaimEventType> CancelledBy { get; set; }
}
Cancels
lists all event types that this event cancels when it appears after them in the list ordered by OccurredOn
. CancelledBy
is the inverse, that is, the event is cancelled if one of the CancelledBy
events appears after it.
How can I query a list of these objects so that items that are cancelled by other items in the list do not appear in the results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常简单,尽管您似乎在重复列出取消和取消的工作:
从集合对象中删除所有元素,其中集合中存在另一个 ClaimEvent,该事件取消此元素类型的 ClaimEvent 并在此声明事件之后发生(即,其中存在是一个或多个这样的元素)。
编辑:具有更易读语法的功能等效代码
这也可以在调用
Exists
中使用第二个委托方法来完成,以查找任何取消事件:资源 strong>
MSDN:List(Of T).RemoveAll 方法
Pretty straightforward, though you seem to be duplicating effort listing both cancels and cancelled by:
Remove all elements from the collection object where there exists another ClaimEvent in the collection that cancels a ClaimEvent of this element's type and occurred after this claim event (i.e. where there are one or more such elements).
EDIT: Functionally equivalent code with more readable syntax
This may also be accomplished using a second delegate method in a call to
Exists
to find any cancelling events:Resources
MSDN: List(Of T).RemoveAll Method
如果我正确理解您的要求,我想您可能会想要这样的东西。它本质上是迭代序列并构建已经存在的事件类型的
HashSet
。对于序列中的每个ClaimEvent
,它会检查先前存在的事件类型以查找当前对象的取消类型之一。如果找不到,它可以生成当前对象并将其类型添加到集合中。...
If I understand your requirement correctly, I think you might want to go for something like this. It essentially iterates over the sequence and builds a
HashSet
of event types already present. For eachClaimEvent
in the sequence, it checks the previously existing event types for one of the current object's cancelling types. If it doesn't find one, it can yield the current object and add its type to the set....