如何使用有效的Enumerable.Repeat

时间:2014-05-13 09:45:46

标签: c# linq linq-expressions

我只想在其他代码位置使用此查询初始化:

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

我已经宣布了一个常见的帖子查询。现在我只希望PublishDate成为default(DateTime?)default(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();

这里我想要不可为空的日期时间

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

这第二个让我无法转换隐式转换类型...

1 个答案:

答案 0 :(得分:1)

您可以将其投射到Nullable<DateTime>

PublishDate = (DateTime?)default(DateTime),

例如,具有一个匿名类型的列表:

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

此处PublishDate01.01.0001 00:00:00而非null