[英]How to select multiple column and distinct one column by LINQ?
I have: 我有:
var names = db.tblPosts.Select(x => new { x.UserID, x.title }).Distinct().ToList();
I want select UserID and title and UserID is distinct. 我想选择UserID和title , UserID是不同的。
but not worked and userID is not distinct.. 但没有工作,userID不明显..
var items = db.tblPosts
.GroupBy(x => x.UserId)
.Select(g => new { UserId = g.Key, Title = g.FirstOrDefault().Title })
.ToList();
It will return first Title
for each UserId
. 它将返回每个
UserId
第一个Title
。 Add additional OrderBy
/ ThenBy
to sort items within group before taking first one. 添加额外的
OrderBy
/ ThenBy
以对第一组中的项目进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.