簡體   English   中英

Linq連續日期需要

[英]Linq continuous date takewhile

主要問題:僅當連續且僅當它們與某些屬性匹配時,才如何按日期對元素進行分組?

詳細資料

給定這種類型的對象:

public class MyObj
{
    public DateTime Date { get; set; }
    public string ConditionalValue1 { get; set; }
    public int ConditionalValue2 { get; set; }
    public int OtherValue { get; set; }

    //for test purposes only, i would like to avoid doing this
    public bool CompareFields(MyObj toBeCompared)
    {
        return ConditionalValue1 == toBeCompared.ConditionalValue1 &&
               ConditionalValue2 == toBeCompared.ConditionalValue2; 
    }
}

我有它們的列表,填充如下:

//list is ordered for semplicity, but it won't be by default
//list is a result of a group by too
var myList = new[]
{
    new MyObj { Date = new DateTime(2009, 10, 20), ConditionalValue1 = "A", ConditionalValue2 = 1, OtherValue = 1 },
    new MyObj { Date = new DateTime(2009, 10, 21), ConditionalValue1 = "A", ConditionalValue2 = 1, OtherValue = 2 },
    new MyObj { Date = new DateTime(2009, 10, 22), ConditionalValue1 = "B", ConditionalValue2 = 1, OtherValue = 3 },
    new MyObj { Date = new DateTime(2009, 10, 23), ConditionalValue1 = "A", ConditionalValue2 = 2, OtherValue = 4 },
    new MyObj { Date = new DateTime(2009, 10, 24), ConditionalValue1 = "A", ConditionalValue2 = 2, OtherValue = 5 },
    new MyObj { Date = new DateTime(2009, 10, 25), ConditionalValue1 = "A", ConditionalValue2 = 1, OtherValue = 6 },
    //Note the hole for day 26
    new MyObj { Date = new DateTime(2009, 10, 27), ConditionalValue1 = "A", ConditionalValue2 = 1, OtherValue = 7},
};

想要的 (偽代碼):

List = 
  { ListOfObjects }, //First has date = "2009/10/20", last has = "2009/10/21" 
  { ListOfObjects }, //First has date = "2009/10/22", last has = "2009/10/22" //reason: doesn't match Value1
  { ListOfObjects }, //First has date = "2009/10/23", last has = "2009/10/24" 
  { ListOfObjects }, //First has date = "2009/10/25", last has = "2009/10/25" //reason: doesn't match Value2
  { ListOfObjects }  //First has date = "2009/10/27", last has = "2009/10/27" //reason: there is no "2009/10/26" object

到目前為止的測試

//i pick the first element, to be compared with the next ones
var firstInList = myList.First();
//take while date is consequent and fields are compared
var partialList = myList
    .TakeWhile(
        (obj, index) => obj.Date == firstInList.Date.AddDays(index) &&
                        obj.CompareFields(firstInList)
    );

這部分起作用,因為它當然只會為第一個匹配的元素返回正確的結果。

我可以Skip(count)列表直到0個元素,但是我想使用IEqualityComparer而不是CompareFields方法來使用group by

您可能可以使用Enumerable.GroupBy 但是,我覺得它可能會遍歷整個場景。 如果是這樣,您總是可以創建自己的SequencedGroupBy擴展方法。

警告:不適用於並行擴展(AsParallel)

DateTime lastGroupDate = DateTime.MinValue;
DateTime lastDate = DateTime.MinValue;

IEnumerable<IGrouping<DateTime, MyObj>> groups = myList
    .GroupBy(o =>
        {
            if (lastGroupDate != DateTime.MinValue &&
                o.Date.AddDays(-1) == lastDate)
            {
                lastDate = o.Date;
            }
            else
            {
                lastGroupDate = o.Date;
                lastDate = o.Date;
            }

            return lastGroupDate;
        }
    );

foreach(IGrouping<DateTime, MyObj> grouping in groups)
{
    // grouping.Key == the grouped date / first in sequence

    foreach(MyObj obj in grouping)
    {
        // obj.Date == actual date
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM