I am using LINQ. I have a list in which there some duplicate record based on a particular column value . I want to get unique record based on particular value from a list using Linq . I am using distict method to get the unique record but it is not supported. Here is my code.
private List<CountOnlineUserModel> GetUserList()
{
List<CountOnlineUserModel> Users = new List<CountOnlineUserModel>();
CountOnlineUserModel p = new CountOnlineUserModel();
p.UserUniqueID = "IMW3";
p.Username = "Rahul";
Users.Add(p);
p.UserUniqueID = "IMW4";
p.Username = "Raheem";
Users.Add(p);
p.UserUniqueID = "IMW3";
p.Username = "Rahul";
Users.Add(p);
p.UserUniqueID = "IMW4";
p.Username = "Raheem";
Users.Add(p);
return Users;
}
Try implementing IEquatable<CountOnlineUserModel>
in CountOnlineUserModel
.
public class CountOnlineUserModel : IEquatable<CountOnlineUserModel> {
// ...
public bool Equals(CountOnlineUserModel other) {
return UserUniqueID == other.UserUniqueID;
// use the above if you just want to compare the IDs. Use below if you want to compare both the ID and name
return UserUniqueID == other.UserUniqueID &&
Username == other.Username;
}
}
Now you can do Distinct
:
CountOnlineUserModel p = new CountOnlineUserModel();
p.UserUniqueID = "IMW3";
p.Username = "Rahul";
Users.Add(p);
p = new CountOnlineUserModel();
p.UserUniqueID = "IMW4";
p.Username = "Raheem";
Users.Add(p);
p = new CountOnlineUserModel();
p.UserUniqueID = "IMW3";
p.Username = "Rahul";
Users.Add(p);
p = new CountOnlineUserModel();
p.UserUniqueID = "IMW4";
p.Username = "Raheem";
Users.Add(p);
Console.WriteLine(Users.Distinct());
You can do GroupBy:
return Users.GroupBy(c => c.UserUniqueID).Select(c => c.First()).ToList();
Make sure to add this on your using statements if you want to use GroupBy or Distinct:
using System.Linq;
You can use GroupBy()
.
List<CountOnlineUserModel> distinctOnlineUsers = GetUserList()
.GroupBy(p => p.UserUniqueID)
.Select(g => g.First())
.ToList();
You can groups on multiple properties as well, can be done as follows:
List<CountOnlineUserModel> distinctOnlineUsers = GetUserList()
.GroupBy(p => new {p.UserUniqueID, p.Username} )
.Select(g => g.First())
.ToList();
First of all, your sample data is messed up. You have created a list containing four references, all to the same object (whose properties you set once and then overwrite three times). Unless you are doing something pretty unusual, you have to instantiate a new object for each entry:
private List<CountOnlineUserModel> GetUserList()
{
var users = new List<CountOnlineUserModel>
{
new CountOnlineUserModel
{
UserUniqueID = "IMW3",
Username = "Rahul"
},
new CountOnlineUserModel
{
UserUniqueID = "IMW3",
Username = "Rahul"
},
new CountOnlineUserModel
{
UserUniqueID = "IMW4",
Username = "Raheem"
},
new CountOnlineUserModel
{
UserUniqueID = "IMW3",
Username = "Rahul"
}
}
}
Second, I think you may be overthinking the problem. With the sample data you have provided, if you just want one of the records that matches on a particular value, you don't need distinct or group by. Just take the first record that matches.
var record = users.Where( u => u.UserUniqueID == "IMW3" ).First();
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.