Linq 自连接和过滤

发布于 2024-11-12 04:28:58 字数 1075 浏览 5 评论 0原文

我有一个由此类组成的 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 技术交流群。

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

发布评论

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

评论(2

暮年 2024-11-19 04:28:58

非常简单,尽管您似乎在重复列出取消和取消的工作:

List<ClaimEvent> theList = new List<ClaimEvent>();

theList.RemoveAll(i => (from j in theList
                        where j.ClaimEventClaimEventType.Cancels.Contains(i.ClaimEventClaimEventType) &&
                        j.OccurredOn > i.OccurredOn
                        select j).Count() > 0);

从集合对象中删除所有元素,其中集合中存在另一个 ClaimEvent,该事件取消此元素类型的 ClaimEvent 并在此声明事件之后发生(即,其中存在是一个或多个这样的元素)。

编辑:具有更易读语法的功能等效代码

这也可以在调用Exists中使用第二个委托方法来完成,以查找任何取消事件:

theList.RemoveAll(i =>
    theList.Exists(j =>
        j.ClaimEventClaimEventType.Cancels.Contains(i.ClaimEventClaimEventType) &&
        j.OccurredOn > i.OccurredOn));

资源 strong>

MSDN:List(Of T).RemoveAll 方法

Pretty straightforward, though you seem to be duplicating effort listing both cancels and cancelled by:

List<ClaimEvent> theList = new List<ClaimEvent>();

theList.RemoveAll(i => (from j in theList
                        where j.ClaimEventClaimEventType.Cancels.Contains(i.ClaimEventClaimEventType) &&
                        j.OccurredOn > i.OccurredOn
                        select j).Count() > 0);

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:

theList.RemoveAll(i =>
    theList.Exists(j =>
        j.ClaimEventClaimEventType.Cancels.Contains(i.ClaimEventClaimEventType) &&
        j.OccurredOn > i.OccurredOn));

Resources

MSDN: List(Of T).RemoveAll Method

往事风中埋 2024-11-19 04:28:58

如果我正确理解您的要求,我想您可能会想要这样的东西。它本质上是迭代序列并构建已经存在的事件类型的HashSet。对于序列中的每个 ClaimEvent,它会检查先前存在的事件类型以查找当前对象的取消类型之一。如果找不到,它可以生成当前对象并将其类型添加到集合中。

public static IEnumerable<ClaimEvent> GetUncancelledEvents(this IEnumerable<ClaimEvent> source)
{   
    // note: override Equals & GetHashCode in ClaimEventType**
    HashSet<ClaimEventType> existingEventTypes = new HashSet<ClaimEventType>();

    foreach (var @event in source)
    {
        bool isCancelled = false;
        foreach (var cancellingEvent in @event.ClaimEventClaimEventType.CancelledBy)
        {
            if (existingEventTypes.Contains(cancellingEvent))
            {
                isCancelled = true;
                break;
            }
        }

        if (!isCancelled)
        {
            existingEventTypes.Add(@event.ClaimEventClaimEventType);
            yield return @event;
        }
    }
}

...

var uncancelledEvents = eventsList.GetUncancelledEvents();

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 each ClaimEvent 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.

public static IEnumerable<ClaimEvent> GetUncancelledEvents(this IEnumerable<ClaimEvent> source)
{   
    // note: override Equals & GetHashCode in ClaimEventType**
    HashSet<ClaimEventType> existingEventTypes = new HashSet<ClaimEventType>();

    foreach (var @event in source)
    {
        bool isCancelled = false;
        foreach (var cancellingEvent in @event.ClaimEventClaimEventType.CancelledBy)
        {
            if (existingEventTypes.Contains(cancellingEvent))
            {
                isCancelled = true;
                break;
            }
        }

        if (!isCancelled)
        {
            existingEventTypes.Add(@event.ClaimEventClaimEventType);
            yield return @event;
        }
    }
}

...

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