Linq回归第一次约会

时间:2011-12-21 08:00:52

标签: c# asp.net linq linq-to-sql

我有查询:

var q = db.tblArcadeGamePlays
    .Where(c => c.GameID == GameID && c.ReferalID != 0 && c.ReferalID != null)
    .GroupBy(r => new { r.ReferalID })
    .Select(g => new { Plays = g.Count(), URL = g.Key.ReferalID })
    ;       

tblArcadeGamePlays还有一个字段Date,在该组中,我想返回该组包含的最早观察日期。

我该怎么做?

3 个答案:

答案 0 :(得分:6)

.Select(g => new {
       Plays = g.Count(), URL = g.Key.ReferalID,
       MinDate = g.Min(x => x.Date
});

可能也会在这里找到更容易阅读/维护的LINQ语法,但是......扩展语法也会起作用。但是作为LINQ:

var q = from c in db.tblArcadeGamePlays
        where c.GameID == GameID && c.ReferalID != 0 && c.ReferalID != null
        group c by c.ReferalID into g
        select new {
            Plays = g.Count(), URL = g.Key.ReferalID, MinDate = g.Min(x=>x.Date)
        };

答案 1 :(得分:1)

这可能有效

var firstIndex = q.Min(c => c.Date);

答案 2 :(得分:1)

var q = db.tblArcadeGamePlays     .Where(c => c.GameID == GameID && c.ReferalID != 0 && c.ReferalID != null)     .GroupBy(r => new { r.ReferalID })     .Select(g => new { Plays = g.Count(), URL = g.Key.ReferalID , Date = g.Min(cc=>cc.DateField) })     ; 

将DateField替换为您的实际属性