-1

我正在搜索带有日期时间类型键和值作为对象列表的排序字典。我需要找到字典中每个对象的最新值(基于对象的属性)。我的对象有 3 个属性:名称、值和创建日期。我的字典按最新日期降序排列。

我已经以某种方式完成了这项工作,但我确信使用 LINQ 有一个捷径。请注意,我使用的是.NET 3.5。能否请你帮忙?请不要被下面的大量代码推迟,因为为了清楚起见,我添加了它,我只是在寻找一个 linq 查询来查询列表对象列表。

下面的代码:

public void Should_link_recent_data_together()
{
    var data = TimeSeriesDataFactoryEx.GetData();

    var allAttributes = new List<string>()
    {
        TimeSeriesConstants.TOTAL_COST_CODE,
        TimeSeriesConstants.TOTAL_VALUE_CODE,
        TimeSeriesConstants.SOURCE_CODE
    };

    var latestList = new List<TimeSeries>();

    var allValues = data.Values.ToList();


    #region HOW DO I DO THIS USING LINQ?

    bool found = false;

    foreach (var attribute in allAttributes)
    {
        found = false;
        foreach (var tsData in allValues)
        {
            foreach (var ts in tsData)
            {
                if (ts.MetricName == attribute && !string.IsNullOrEmpty(ts.MetricValue))
                {
                    latestList.Add(ts);
                    found = true;
                    break;
                }
            }
            if (found)
                break;
        }
    }

    #endregion

    Assert.IsTrue(latestList.Count == 3);
    Assert.IsTrue(latestList.Where(x => x.MetricName == TimeSeriesConstants.TOTAL_COST_CODE).First().MetricValue == "1");
    Assert.IsTrue(latestList.Where(x => x.MetricName == TimeSeriesConstants.TOTAL_VALUE_CODE).First().MetricValue == "2");
    Assert.IsTrue(latestList.Where(x => x.MetricName == TimeSeriesConstants.SOURCE_CODE).First().MetricValue == "gp");
    Assert.IsTrue(latestList.Where(x => x.MetricName == TimeSeriesConstants.SOURCE_CODE).First().Quarter == DateTime.Today.AddMonths(-3));

}



internal class TimeSeriesDataFactoryEx
{
    public static SortedDictionary<DateTime?,List<TimeSeries>> GetData()
    {
        return new SortedDictionary<DateTime?, List<TimeSeries>>(new DateComparer())
        {
            {
                DateTime.Today, new List<TimeSeries>()
                {
                    new TimeSeries()
                    {
                        Quarter = DateTime.Today,
                        MetricValue = "1",
                        MetricName = TimeSeriesConstants.TOTAL_COST_CODE
                    },
                    new TimeSeries()
                    {
                        Quarter = DateTime.Today,
                        MetricValue = "2",
                        MetricName = TimeSeriesConstants.TOTAL_VALUE_CODE
                    },
                    new TimeSeries()
                    {
                        Quarter = DateTime.Today,
                        MetricValue = "",
                        MetricName = TimeSeriesConstants.SOURCE_CODE
                    }
                }
            },
            {
                DateTime.Today.AddMonths(-3), new List<TimeSeries>()
                {
                    new TimeSeries()
                    {
                        Quarter = DateTime.Today.AddMonths(-3),
                        MetricValue = "3",
                        MetricName = TimeSeriesConstants.TOTAL_COST_CODE
                    },
                    new TimeSeries()
                    {
                        Quarter = DateTime.Today.AddMonths(-3),
                        MetricValue = "4",
                        MetricName = TimeSeriesConstants.TOTAL_VALUE_CODE
                    },
                    new TimeSeries()
                    {
                        Quarter = DateTime.Today.AddMonths(-3),
                        MetricValue = "gp",
                        MetricName =TimeSeriesConstants.SOURCE_CODE
                    }
                }
                }

        };
    }
}
4

1 回答 1

0

所以,假设我理解你的问题是正确的,假设你有一本这样的字典:

{ Key = "1/1/1900", Value = List Of Objects, of which each has a DateTimeProperty }
...
{ Key = "1/4/1900", Value = List Of Objects, of which each has a DateTimeProperty }

你正在寻找从你的字典中找到一组对象,它是每个键的最新时间,然后你可以用 linq 很简单地做到这一点:

var latestItems = data.SelectMany(kvp => 
    kvp.Value.OrderByDescending(value => value.Quarter).Take(1)
);

此查询在每个存储桶中查找最新的对象,然后将其作为单个集合(不是可枚举的枚举)返回。SelectMany只要您IEnumerable从该回调中返回一个,您就可以根据需要更改其中的选择器以在每个集合中查找元素。

于 2013-04-24T19:25:31.107 回答