开发者

How to use Lambda expression to format date

开发者 https://www.devze.com 2023-04-12 11:42 出处:网络
New to Lambda. This question is specifically on Lambda expression to manipulate date formatting. The below code used to get all the reminders from scheduler system:

New to Lambda. This question is specifically on Lambda expression to manipulate date formatting. The below code used to get all the reminders from scheduler system:

MyReminders = ScheduledActionService.GetActions().Where(
    a => a.BeginTime.Date == Today
);
  1. It is possible to change the date format before returning the result set.

    .where (
        a => a.BeginTime.Date == Today).Select(
            //the date and format
            BeginTime.date.ToString("d", new cultureInfo("zh-CN")) 
    )
    
  2. Change the Date format as follows but will this work in the lambda expression?

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

Thanks and appreciate your help.

------------ Update :

I tried these two methods. There is no result being displayed in the ListBox :

This scheduler system is for windows phone 7. This reminder object contains Following properties: 1) BeginTime,2) ExpirationTime,3) Title,4) Content,5) isSchedule and ohers

After retrieving, I need to DataBind it to a ListBox . 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 "/>开发者_如何学C
                       < 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>



I don't think you quite understand what a lambda is. At it's heart, a lambda is exactly the same as (in your case)

delegate bool FilterMyObject(MyObject)

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

....

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

it will compile down to something very similar (except it will use the generic delegate Func).

Now if you are using Linq2Sql or something else where the parameter is of type Expression<Func<T,bool>> there is an added step where the person who implemented that where method has a chance to examine what you're trying to do (and usually optimize it).

To answer your question, you probably want

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();

Which will output an array of formatted strings


You can select the items that pass your predicate in Where, you can project those into a different sequence of types, but you can (should) not change the items in the sequence.

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

In this example, the lambda is a Func<YourType, string>, where YourType is the type of the elements in the sequence and string is the output of the expression. That is to say it takes the input (YourType) on the left side item => and produces the output (string) on the right item.BeginDate.ToString().

This will give you an IEnumerable<string> consisting of the BeginDate values in the string format of your choice. Alternately, you could project into an another type

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

This will give you an IEnumerable<AnonymousType> that you can then loop over and access the DateString, Foo, and Bar properties. And, of course, the lambda will represent a Func<YourType, AnonymousType>.

0

精彩评论

暂无评论...
验证码 换一张
取 消