1

私はList<ClaimEvent>このクラスで構成されています:

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。は逆です。つまり、イベントの 1つがその後に現れるCancelledByと、イベントはキャンセルされます。CancelledBy

これらのオブジェクトのリストをクエリして、リスト内の他のアイテムによってキャンセルされたアイテムが結果に表示されないようにするにはどうすればよいですか?

4

2 に答える 2

2

キャンセルとキャンセルの両方をリストする努力を複製しているようですが、かなり簡単です:

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 がコレクション内に存在し、このクレーム イベントの後に発生したコレクション オブジェクトからすべての要素を削除します (つまり、そのような要素が 1 つ以上ある場合)。

編集: より読みやすい構文を持つ機能的に同等のコード

これは、 への呼び出しで 2 番目のデリゲート メソッドを使用して、Existsキャンセル イベントを見つけることもできます。

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

資力

MSDN: List(Of T).RemoveAll メソッド

于 2011-06-02T21:20:13.770 に答える
1

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();
于 2011-06-03T01:49:43.720 に答える