2011-10-13 671 views
1

Lambda的新功能。這個問題特別針對Lambda表達式來操作日期格式。用於獲取所有從調度系統提醒下面的代碼:如何使用Lambda表達式來格式化日期

MyReminders = ScheduledActionService.GetActions().Where(
    a => a.BeginTime.Date == Today 
); 
  1. 有可能返回結果集之前更改日期格式。

    .where (
        a => a.BeginTime.Date == Today).Select(
         //the date and format 
         BeginTime.date.ToString("d", new cultureInfo("zh-CN")) 
    ) 
    
  2. 更改日期格式如下,但這將工作在lambda表達式嗎?

    BeginTime.Date.ToString("d",new cultureInfo("zh-CN"), 
    

感謝,並感謝您的幫助。

------------更新:

我嘗試這兩種方法。在列表框中沒有顯示任何結果:

此調度程序系統用於windows phone 7.此提醒對象包含以下屬性: 1)BeginTime,2)ExpirationTime,3)Title,4)Content,5)isSchedule和其他

檢索後,我需要DataBind它到一個列表框。 ReminderListBox.ItemsSource = MyReminders;

1) var czech = new CultureInfo("zh-CN");

 var MyReminders = ScheduledActionService.GetActions<Reminder>() 
      .Where(a => a.BeginTime.Date == Today) 
      .Select(a => 
      new 
      { 
      Begindate = a.BeginTime.Date.ToString("d", czech), 
      Title = a.Title, 
      Content = a.Content 
      }); 

2) var czech = new CultureInfo("zh-CN"); var MyReminders = ScheduledActionService.GetActions() .Where(a => a.BeginTime.Date == Today) .Select(a => a.BeginTime.Date.ToString("d", czech));

 

    < ListBox Name="ReminderListBox" > 
     < ListBox.ItemTemplate> 
      < DataTemplate> 
      < Grid Background="Transparent" Margin="0,0,0,30"> 

      < StackPanel Orientation="Vertical" > 
       < TextBlock FontSize="23" Text="{Binding Title}"/ > 

        < TextBlock FontSize="23" Text="{Binding Content}" /> 

        < StackPanel Orientation="Horizontal"> 
         < TextBlock Text="begin "/> 
         < TextBlock x:Name="txtDate" Text="{Binding BeginTime}" /> 
         < /StackPanel> 

        < StackPanel Orientation="Horizontal"> 
         < TextBlock Text="expiration "/> 
         < TextBlock Text="{Binding ExpirationTime}"/> 
         < /StackPanel> 
        < StackPanel Orientation="Horizontal"> 
         < TextBlock Text="recurrence "/> 
        < TextBlock Text="{Binding RecurrenceType}" /> 
        < /StackPanel> 
         < StackPanel Orientation="Horizontal"> 
         < TextBlock Text="is scheduled? "/> 
         < TextBlock Text="{Binding IsScheduled}"/> 

          < /StackPanel> 

            < /StackPanel> 

           < /Grid> 
          < /DataTemplate> 
         < /ListBox.ItemTemplate> 
        < /ListBox> 
 

回答

1

您可以選擇是通過你的謂語Where的項目,你可以項目那些進入的類型不同的序列,但你可以(應該)不是變化序列中的項目。

var results = yourSequence.Where(yourPredicate) 
          .Select(item => item.BeginDate.ToString("yourformat")); 

在這個例子中,拉姆達是Func<YourType, string>,其中YourType是元素的序列中的類型和string是表達式的輸出。也就是說,它將輸入(YourType)放在左側item =>,並在右側item.BeginDate.ToString()上生成輸出(string)。

這會給你一個IEnumerable<string>由你選擇的字符串格式的BeginDate值組成。或者,你能預料到的另一種類型的

var results = yourSequence.Select(item => 
      new 
      { 
       DateString = item.BeginDate.ToString("yourformat"), 
       Foo = item.Foo, 
       Bar = item.Bar 
      }); 

這會給你一個IEnumerable<AnonymousType>,然後就可以遍歷並訪問DateStringFooBar性能。當然,拉姆達將代表Func<YourType, AnonymousType>

1

我不認爲你完全理解lambda是什麼。在它的心臟,一個lambda是完全一樣(在你的情況下)

delegate bool FilterMyObject(MyObject) 

bool WhereToday(MyObject obj) { 
    return obj.BeginTime.Date == Today 
} 

.... 

MyReminders = ScheduledActionService.GetActions().Where(new FilterMyObject(whereToday)) 

它將編譯到非常相似的(除了它會使用泛型委託Func鍵)的東西。

現在,如果您使用的是Linq2Sql或其他參數類型爲Expression<Func<T,bool>>的其他地方,那麼執行where方法的人有機會檢查您嘗試執行的操作(並且通常會優化它)。

要回答你的問題,你可能想

var czech = new CultureInfo("zh-CN"); 
var MyReminders = ScheduledActionService.GetActions() 
       .Where(a => a.BeginTime.Date == Today); 
       .Select(a => a.BeginTime.Date.ToString("d",czech) 
       .ToArray(); 

將輸出格式的字符串

數組