开发者

How do i convert the fields of type DateTime to just Date at runtime of an IQueryable of records?

I am in a situation where i need to convert fields with DateTime to a Date (DateTime (2011-01-01 00:00:00 ) to Date ( 2011-01-01 just a string with no time )) before i assign the source to the DataGrid ( without using Binding/Converters ) the collection is an IQueryable but not sure how to do it.

Let me repeat I want the collection to be manipulated even before its assigned to DataG开发者_开发问答rid Source

can someone help me? Thanks


If the problem is just converting a DateTime to a string representation, you can use ToString() and provide a formatting string:

DateTime date = new DateTime(2011,01,01);
string dateAsString = date.ToString("YYYY-mm-dd");

See the MSDN page for Standard Date and Time Format Strings to see how to control the format.

You can also do the conversion while binding, i.e. bind to a DateTime field, but specify a string format like so (in XAML):

<TextBlock Text="{Binding Path=YourDateTimeField, StringFormat={}{0:d}"/>

If you're asking something else, about binding to an IQueryable, for example, we'll need to see some code that demonstrates what you're trying to accomplish.


Why can't

var myGridData = myCollection.Select(c => new { TheDate = c.MyDate.ToShortDateString(), Field3 = c.SomethingElse, ... });

work?


Just format the DateTime items accordingly:

var results = timeCollection.Select(x => x.ToString("yyyy-MM-dd"));


I guess I'm confused by your question, given that there is no Date type in C#. If you're just trying to get a DataGrid to display a DateTime as a string that has a date format, you'll have to convert it before you bind. Either use a converter or bind against an object that has a pre-formatted date string in that field.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜