简体   繁体   中英

Linq -How to get distinct of multiple fields from Anonymous type

userdetails class is defined:

class userdetails 
{      
 public string FromUserId;
 public string ToUserID;
 ... more properties
}  
var values = userdetails .Select(i => new { i.FromUserID, 
             i.ToUserID}).Distinct();

I want to get list of all the distinct userids from both the fields.

If I understand your question correctly, you want a single list containing unique values from FromUserId and ToUserId fields. In that case you want to union lists of each ID type.

var userDetails = new List<userdetails>();
// fill list with values.

var values = userDetails.Select(i => i.FromUserID).Union(userDetails.Select(i => i.ToUserID));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM