查询动态对象列表的最大值和最小值 [英] Quering a Dynamic Object List for Max and Min Values

查看:90
本文介绍了查询动态对象列表的最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码返回动态对象.我想全部查询它们,并获取保存在created_at中的最小日期和最大日期.我将如何查询呢?

I have the following code which returns the dynamic object. I would like to query them all and get the min date and max date, which is held in created_at. How would I query this?

当前,我正在使用foreach循环,并进行比较和设置日期.有没有更有效的方法可以做到这一点,我将如何去做呢?我知道它的问题,但无法理解如何实际执行它的概念.

Currently I am using a foreach loop, and doing comparisons and setting the dates. Is there a more efficient way to do this and how would I go about doing this? I know its linq but cant get my head around the concept of how to actually do it.

dynamic dynObj = JsonConvert.DeserializeObject(Data);
{
    "created_at": "Fri Sep 27 02:00:08 +0000 2013",
    "id": 
},

推荐答案

string Data = @"[{
    ""created_at"": ""Fri Sep 27 02:00:08 +0000 2013"",
    ""id"": 1
},
{
    ""created_at"": ""Sat Sep 28 02:00:08 +0000 2013"",
    ""id"": 1
}]";
IEnumerable<dynamic> d = (dynamic)JsonConvert.DeserializeObject(Data);
var dates = d.Select(x => DateTime.ParseExact(x.created_at.ToString(),
    "ddd MMM dd HH:mm:ss K yyyy", CultureInfo.InvariantCulture))
    .Cast<DateTime>().ToList();
var maxDate = dates.Max();
var minDate = dates.Min();

这篇关于查询动态对象列表的最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆