Search code examples
entity-frameworklistlinqlinq-group

Return List<string> after group by


I have a function

public List<string> UserList()
{
    var q = from i in _dataContext.PageStat group i by new { i.UserName } into ii select new { ii.Key.UserName };
}

How can I return List<string>?


Solution

  • It looks like you just want the distinct set of usernames... why not just use:

    return _dataContext.PageStat.Select(u => u.UserName)
                                .Distinct()
                                .ToList();
    

    If you really want to use grouping, you could do:

    var q = from i in _dataContext.PageStat
            group i by i.UserName into ii
            select ii.Key;
    return q.ToList();
    

    You don't need all those anonymous types :)