使用Linq获取列表中的月份 [英] Fetch months inside list using Linq

查看:176
本文介绍了使用Linq获取列表中的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的名单

 public class Result
{
    public string id { get; set; }
    public string name { get; set; }
    public string startDate { get; set; } //smaple date 2014-03-31T12:30:03
}


List<Result>

现在我想获取此列表startDate中的所有月份名称.我尝试过类似的操作

Now I want to get all month names that's inside startDate of this list.And I tried some thing like this

List<string> mnamesList= result.Select(s => Convert.ToDateTime(s.startDate).ToString("MMMM")).ToList();

如您所见,此循环将返回重复的月份名称,我不知道如何使用Linq仅获取不同的飞蛾.有人能指出正确的方法吗?

As you can see this loop will return repeated month names, I don't know how to fetch only different moths using Linq. Can any one point out the correct way?

推荐答案

我认为您可以如下使用Linq Distinct()函数:

I think you can use the Linq Distinct() function as follows:

List<string> mnamesList= result.Select(s => Convert.ToDateTime(s.startDate).ToString("MMMM")).Distinct().ToList();

这篇关于使用Linq获取列表中的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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