【问题标题】:Not sure what type it should be不知道应该是什么类型
【发布时间】:2012-08-01 08:27:11
【问题描述】:

我想提取当月的所有星期天,并有这个代码:

 private string GetDatesOfSundays(DateTime DatMonth)
 {
  string sReturn = "";
  int iDayOffset = DatMonth.Day - 1;
  DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
  DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
  while (DatMonth < DatMonth2)
  {
    if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
    {
      if (sReturn.Length > 0) sReturn += ",";
      sReturn += DatMonth.ToShortDateString();
    }
    DatMonth = DatMonth.AddDays(1.0);
  }
  return sReturn;
}   

[HttpGet]
public ActionResult TradeUKKPISearchesData()
{
  string allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);  
  //var reportData = _reportingService.GetTradeUKKPISearches();
  //return View(reportData);
}

问题在于我的 allSundaysInMonth 类型字符串,当然也是空的。 sReturn 是字符串类型,但我又传递了一个日期(我知道:))但是 allSundaysInMonth 应该是什么类型? sReturn 确实有正确的日期...我需要在控制器视图的下拉列表中显示这些日期,以便用户可以选择他/她需要为其运行报告的任何星期日。

谢谢

【问题讨论】:

  • IEnumerable&lt;DateTime&gt; 我觉得不错
  • 我试过了: IEnumerable allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);它给出了错误无法将类型'string'隐式转换为'System.Collections.Generic.IEnumerable':(
  • @charlie,你可以将你的变量声明为string,将方法的返回类型声明为IEnumerable&lt;DateTime&gt;,但不要指望这两者可以结合在一起。一个是日期序列,另一个是一堆字符。根本不是相同的类型,因此您不能将一种分配给另一种。

标签: c#


【解决方案1】:

您可以选择DateTime[]IEnumerable&lt;DateTime&gt;

你的方法签名应该是

private IEnumerable<DateTime> GetDatesOfSundays(DateTime DatMonth)

private DateTime[] GetDatesOfSundays(DateTime DatMonth)

如果您还没有与 IEnumerable 合作过,那就去吧

private DateTime[] GetDatesOfSundays(DateTime DatMonth)
{
    List<DateTime> lst = new List<DateTime>();

    DatMonth = DatMonth.AddDays(-DatMonth.Day + 1);
    DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));

    while (DatMonth < DatMonth2)
    {
        if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
        {
            lst.Add(DatMonth);
            DatMonth = DatMonth.AddDays(7);
            continue;
        }

        DatMonth = DatMonth.AddDays(1);
    }

    return lst.ToArray();
}

并将其称为

DateTime[] allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);

【讨论】:

  • 刚刚建立了一个列表,为什么还要返回一个数组呢? IList&lt;DateTime&gt; 会更简单。
  • 请再问一个问题..我使用上面的示例,返回 lst.ToArray();包含正确的信息,但调用行: DateTime[] allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);不,所有SundaysInMonth 中什么都没有?
  • @charlie_cat:它在我这边工作正常。 allSundaysInMonth 获得本月 4 个日期,即 5、12、19、26。我修改了我的答案以更快。请检查并让我知道。也告诉我你在哪一个月和哪一年什么都没有。
  • 我添加了额外的两行,但我的变量中仍然没有任何内容,是的,这些是返回 lst.ToArray(); 中的日期;谢谢..虽然有些地方不对...我会再次检查我的代码。谢谢
  • @charlie_cat:这是在发布模式还是调试模式下运行?
【解决方案2】:

怎么样

private IEnumerable<DateTime> GetDatesOfSundays(DateTime DatMonth)
{
    int iDayOffset = DatMonth.Day - 1;   
    DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
    DateTime DatMonth2 =
        DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
    while (DatMonth < DatMonth2)
    {
        if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
        {
            yield return DatMonth;
        }

        DatMonth = DatMonth.AddDays(1.0);
    }
}

我很想将您的函数重写为类似这样的扩展

public static IEnumerable<Datetime> DaysOfMonth(
    this DateTime any,
    DayOfWeek day)
{
    // start at first of month
    var candidate = new DateTime(any.Year, any.Month, 1);

    var offset = (int)day - (int)candidate.DayOfWeek;

    if (offset < 0)
    {
        offset += 7
    }

    candidate = candidate.AddDays(offset);

    while (cadidate.Month == any.Month)
    {
        yield return candidate;
        candidate = candidate.AddDays(7.0)
    }
}

那么你可以这样使用它

var allSundaysInMonth = DateTime.Now.DaysOfMonth(DayOfWeek.Sunday);

如果您想将IEnumerable&lt;DateTime&gt; 转换为string,您可以这样做,

var listOfDates = string.Join<DateTime>(", ", allSundaysInMonth);

使用thisstring.Join重载


如果你真的想要DateTime[] 的日子,你可以这样做(但没有必要)

DateTime[] allSundaysInMonth = GetDatesOfSundays(DateTime.Now).ToArray();

或者对于我的扩展示例

var allSundaysInMonth = DateTime.Now.DaysOfMonth(DayOfWeek.Sunday).ToArray();

【讨论】:

  • oooo 好的抱歉,我从未使用过 IEnumerable..我现在明白/明白了..谢谢!
  • @charlie_cat:那就看看我的回答吧。
  • @charlie_cat 了解IEnumerable,这样你就可以释放 linq 的力量
  • @charlie_cat 我优化了扩展方法以避免不必要的迭代。
  • @charlie_cat 最后一次调整。
猜你喜欢
  • 2020-09-26
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 2013-06-02
相关资源
最近更新 更多