-1
    var users = from u in db.Users.Include("UserLogins")
      join ul in db.UserLogins on u.UserId equals ul.UserId
      join ua in db.UserActions ON ul.UserLoginId = ua.UserLoginId
      where u.IsActive = true
      group new { ul, ua }
        by new {u.UserId} into proj

    select new 
    {
    UserId = (Int32)y.Key.UserId,
        LoginDates = ????????
    };

是否可以在投影中返回日期列表的集合?

我猜不是,用逗号分隔的日期列表怎么样?

就像是:

"DateTime1, Datetime2, DateTime3"
4

2 回答 2

0

我希望我能正确理解你的问题......

var users =
  from u in db.Users
  where u.IsActive
  select new
  {
    u.UserId,
    LoginDates =
      from d in u.LoginDates
      select d.LoginDate,
  };

这将使LoginDatesanIEnumerable成为任何类型LoginDate(可能IEnumerable<DateTime>)。如果愿意,您可以在之后创建一个逗号分隔的列表。

于 2013-06-10T20:25:42.823 回答
0

是否可以在投影中返回日期列表的集合?

当然,每个分组都实现IEnumerable了,所以你可以使用ToList

select new 
{
    UserId = (Int32)y.Key.UserId,
    LoginDates = y.Select( i => i.LoginDate).ToList()
};

逗号分隔的日期列表怎么样?

只需使用String.Join

var users = (from u in db.Users.Include("UserLogins")
  join ul in db.UserLogins on u.UserId equals ul.UserId
  join ua in db.UserActions ON ul.UserLoginId = ua.UserLoginId
  where u.IsActive = true
  group new { ul, ua }
    by new {u.UserId} into proj)
.AsEnumerable()  // Hydrate the query
.Select(y=> new 
{
    UserId = (Int32)y.Key.UserId,
    LoginDates = string.Join(",",
                             y.Select( i => i.LoginDate
                                             .ToString("yyyy-MM-dd")
                                     ).ToArray()
                            )
});
于 2013-06-10T20:28:33.143 回答