开发者

Return List<string> after group by

开发者 https://www.devze.com 2023-01-05 19:50 出处:网络
I have a function p开发者_如何学Pythonublic List<string> UserList() { var q = from i in _dataContext.PageStat group i by new { i.UserName } into ii select new { ii.Key.UserName };

I have a function

p开发者_如何学Pythonublic 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>?


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消