Linq查询用于搜索具有列表列表作为值的字典内的对象

时间:2013-04-24 19:20:30

标签: c# linq

我正在使用datetime类型的键和值作为对象列表搜索已排序的字典。我需要找到的是字典中每个对象的最新值(基于对象的属性)。我的对象有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
                    }
                }
                }

        };
    }
}

1 个答案:

答案 0 :(得分: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,就可以根据需要查找每个组中的元素。