-1

你如何将这段代码(在 Visual Basic 上)翻译成 C#

For m = 1 To 12
     Dim daysInMonth = curCulture.Calendar.GetDaysInMonth(year, m)
     Dim firstMonthDate = New Date(year, m, 1)
     Dim daysBefore = (7 + (firstMonthDate.DayOfWeek - firstYearDate.DayOfWeek)) Mod 7
     Dim daysBehind = (tblCalendar.Columns.Count - 2) - (daysBefore + daysInMonth) - 1
     Dim monthDays = From d In Enumerable.Range(1, daysInMonth) Select New With {.Day = d.ToString}
     Dim emptyDaysBefore = From d In Enumerable.Range(1, daysBefore) Select New With {.Day = ""}
     Dim emptyDaysAfter = From d In Enumerable.Range(1, daysBehind) Select New With {.Day = ""}
     Dim montos = From d In Enumerable.Range(1, 2) Select New With {.Day = "0"}
     Dim monthName = curCulture.DateTimeFormat.GetMonthName(m)
     Dim allFields = ({New With {.Day = monthName}}.
                    Union(emptyDaysBefore).
                    Union(monthDays).
                    Union(emptyDaysAfter).
                    Union(montos).
                    Select(Function(d) d.Day)
                    ).ToArray
     tblCalendar.Rows.Add(allFields)
  Next

我不能简单地通过这样做找到联合方法:

for (int _m = 1 ; _m <= 12; _m++) {
     int _daysInMonth = _currentCulture.Calendar.GetDaysInMonth(_selYear, _m);
     DateTime _firstMonthDate = new DateTime(_selYear, _m, 1);
     int _daysBefore = (7 + (_firstMonthDate.DayOfWeek - _firstDateOfYear.DayOfWeek)) % 7;
     int _daysBehind = (_tableCalendar.Columns.Count) - (_daysBefore + _daysInMonth) - 1;
     var _monthDays = from d in Enumerable.Range(1, _daysInMonth) select new { Day = d.ToString() };
     var _emptyDaysBefore = from d in Enumerable.Range(1, _daysBefore) select new { Day = "" };
     var _emptyDaysAfter = from d in Enumerable.Range(1, _daysBehind) select new { Day = "" };
     string _monthName = _currentCulture.DateTimeFormat.GetMonthName(_m);

     var _allFields = (new { 
               Day = _monthDays
           });
}
4

3 回答 3

2

对于代码:

{New With {.Day = monthName}}

这是 VB.NET 中的一个数组 - 它是一个单元素数组,但仍然是一个数组。所以相应的 C# 代码将是:

new[] { new { Day = monthName } };

其余的 LINQ 表达式应该从那里开始工作。

于 2012-10-24T21:31:46.530 回答
0

您不需要在 C# 中使用联合。例如,我可以像这样创建一个匿名类型:

var anon = new {someString = "S", someInt = someLocalIntValue, myCustomClass = this};

http://msdn.microsoft.com/en-us/library/bb384061.aspx

不完全确定这是您正在寻找的,但我希望这会有所帮助。

于 2012-10-24T21:26:46.917 回答
0

我不确定这些工具的效果如何,因为我几乎不需要做这种事情。但是你可以试试http://www.developerfusion.com/tools/convert/vb-to-csharp/

于 2012-10-24T21:22:46.417 回答