Search code examples
c#linqlinq-expressions

how to use effective Enumerable.Repeat


i just want to use this query initialization at other code location :

 var post = Enumerable.Repeat(new
            {
                Id = default(int),
                Title = string.Empty,
                Publisher = string.Empty,
                ToUser = string.Empty,
                PublishDate = default(DateTime?),//need to default(DateTime)
                IsFile = default(bool),
                CategoryName = string.Empty,
                status_name = string.Empty,
                Group_name = string.Empty,
                FileSize = default(long?)
            }, 0).ToList();

I have declared one common post query. Now I just want PublishDate to be default(DateTime?) or default(DateTime).

Can I set it with nullable and also non-nullable? When initializing this query it needs non-nullable datetime.

-------------------------Updated--------------------------

here i want nullable datetime

   post = db.Posts.Where(p => (p.ToUser_id.Equals(user_id) || p.ToUser_id.Equals(null)) && p.User_id != user_id).OrderByDescending(p => p.Sent_Datetime).Select(p => new
                    {
                        Id = p.Id,
                        Title = p.Title,
                        Publisher = db.Users.Where(u => u.Id.Equals(p.User_id)).Select(u => u.First_name + ' ' + u.Last_name).FirstOrDefault(),
                        ToUser = db.Users.Where(u => u.Id.Equals(p.ToUser_id)).Select(u => u.First_name + ' ' + u.Last_name).FirstOrDefault(),
                        PublishDate = p.Sent_Datetime,//nullable date time field
                        IsFile = p.IsFileAttached,
                        CategoryName = db.Categories.Where(c => c.Id.Equals(p.Category_id)).Select(c => c.Category_name).FirstOrDefault(),
                        status_name = db.Status.Where(s => s.Id.Equals(p.status_id)).Select(s => s.status_name).FirstOrDefault(),
                        Group_name = db.Groups.Where(g => g.Id.Equals(p.group_id)).Select(g => g.Group_name).FirstOrDefault(),
                        FileSize = p.TotalFileSize
                    }).FilterForColumn(ColumnName, SearchText).ToList();

here i want non - nullable datetime

 post = db.Posts.Where(p => p.User_id.Equals(user_id) && p.ToUser_id.Equals(user_id) && p.Sent_Datetime.Equals(null)).OrderByDescending(p => p.Created_datetime).Select(p => new
                    {
                        Id = p.Id,
                        Title = p.Title,
                        Publisher = db.Users.Where(u => u.Id.Equals(p.User_id)).Select(u => u.First_name + ' ' + u.Last_name).FirstOrDefault(),
                        ToUser = db.Users.Where(u => u.Id.Equals(p.ToUser_id)).Select(u => u.First_name + ' ' + u.Last_name).FirstOrDefault(),
                        PublishDate = p.Created_datetime,//non-nullable date time field
                        IsFile = p.IsFileAttached,
                        CategoryName = db.Categories.Where(c => c.Id.Equals(p.Category_id)).Select(c => c.Category_name).FirstOrDefault(),
                        status_name = db.Status.Where(s => s.Id.Equals(p.status_id)).Select(s => s.status_name).FirstOrDefault(),
                        Group_name = db.Groups.Where(g => g.Id.Equals(p.group_id)).Select(g => g.Group_name).FirstOrDefault(),
                        FileSize = p.TotalFileSize
                    }).FilterForColumn(ColumnName, SearchText).ToList();

this second gives me can not convert implicitly convert type...


Solution

  • You can cast it to Nullable<DateTime>:

    PublishDate = (DateTime?)default(DateTime),
    

    For example a list with one anonymous type:

    var post = Enumerable.Repeat(new
    {
        Id = default(int),
        Title = string.Empty,
        Publisher = string.Empty,
        ToUser = string.Empty,
        PublishDate = (DateTime?)default(DateTime),
        IsFile = default(bool),
        CategoryName = string.Empty,
        status_name = string.Empty,
        Group_name = string.Empty,
        FileSize = default(long?)
    }, 1).ToList();
    

    Here PublishDate is 01.01.0001 00:00:00 instead of null.