format date in linq query result

后端 未结 4 1006
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 02:42

The following linq to entities query gives the result below:

public class UserCountResult
{
    public DateTime? date { get; set; } // **should this be strin         


        
相关标签:
4条回答
  • 2021-01-18 03:19

    g.Key is nullable because that's the signature of EntityFunctions.TruncateTime. http://msdn.microsoft.com/en-us/library/dd395596.aspx.

    To exit from Linq to Entities, you can leave the query as is, and project it after the fact:

    return Json(query.AsEnumerable().Select(r => new 
        {
            date = r.date.GetValueOrDefault().ToString("dd.MM.yyyy"),
            users = r.users,
            visits = r.visits
        }), JsonRequestBehavior.AllowGet);
    

    It's not pretty, but that's Linq to Entities for you.

    0 讨论(0)
  • 2021-01-18 03:27

    If this is using SQL Server, you can concat the result of SQL functions

    using System.Data.Entity.SqlServer;    
    
    ...
    
    date = SqlFunctions.DatePart("year",g.Key)
        +"-"+SqlFunctions.DatePart("month",g.Key)
        +"-"+SqlFunctions.DatePart("day",g.Key)
    
    0 讨论(0)
  • 2021-01-18 03:28

    Something like this should work:

    date = new Date(parseInt(g.Key.substr(6)));
    

    The substr will pull off the "/Date(" string, parseInt will pull just the integer and Date will give you a new date object.

    EDIT:

    Just found this SO question that supports this answer.

    How do I format a Microsoft JSON date?

    0 讨论(0)
  • 2021-01-18 03:35

    Assuming you're using JSON.NET as the JSON serializer, you can apply the JsonConverterAttribute to the date property to specify a custom converter.

    [JsonConverter(typeof(MyDateConverter))]
    public DateTime? date { get; set; }
    

    You can use the DateTimeConverterBase class as a base class for your converter.

    Here's a possible implementation for MyDateConverter:

    class CustomDateTimeConverter : DateTimeConverterBase
    {
        private const string Format = "dd.MM.yyyy";
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            DateTime d = (DateTime)value;
            string s = d.ToString(Format);
            writer.WriteValue(s);
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (s == null)
                return null;
            string s = (string)reader.Value;
            return DateTime.ParseExact(s, Format, null);
        }
    }
    

    Another option is to exclude the date property from the serialization (using the JsonIgnoreAttribute), and add another property of type String that converts to and from the desired format. Here's an implementation of this solution:

    public class UserCountResult
    {
        [JsonIgnore]
        public DateTime? date { get; set; }
        [JsonProperty("date")]
        public string DateAsString
        {
            get
            {
                return date != null ? date.Value.ToString("dd.MM.yyyy") : null;
            }
            set
            {
                date = string.IsNullOrEmpty(value) ? default(DateTime?) : DateTime.ParseExact(value, "dd.MM.yyyy", null);
            }
        }
    
        public int users { get; set; }
        public int visits { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题