C# 从列表中获取不同的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15086438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get distinct values from list
提问by Ayyappan Sekaran
public List<MAS_EMPLOYEE_TRANSFER> GetEmployeeTransferListForHR(TimecardDataContext TimecardDC)
{
List<MAS_EMPLOYEE_TRANSFER> objEmployeeTransferList = null;
try
{
objEmployeeTransferList = new List<MAS_EMPLOYEE_TRANSFER>();
objEmployeeTransferList = TimecardDC.MAS_EMPLOYEE_TRANSFER.Where(
employee =>
employee.HR_ADMIN_IND=="Y").ToList();
}
finally
{
}
return objEmployeeTransferList;
}
It shows all list of values where hr admin indicator=yes. But I have to get hr admin=yes
and distinct(empid) from the table MAS_EMPLOYEE_TRANSFER
. How to get distinct empId
from the the objEmployeeTransferList
.
它显示 hr admin indicator=yes 的所有值列表。但是我必须hr admin=yes
从 table 中获取和 distinct(empid) MAS_EMPLOYEE_TRANSFER
。如何获得不同empId
从的objEmployeeTransferList
。
采纳答案by Sergey Berezovskiy
List<int> ids = objEmployeeTransferList
.Select(e => e.empId)
.Distinct()
.ToList();
Also you can make this on server side without creating in-memory employee list with all admin records:
您也可以在服务器端进行此操作,而无需创建包含所有管理员记录的内存员工列表:
List<int> ids = TimecardDC.MAS_EMPLOYEE_TRANSFER
.Where(e => e.HR_ADMIN_IND == "Y")
.Select(e => e.empId)
.Distinct()
.ToList();
回答by Habibillah
Have you try:
你有没有试过:
objEmployeeTransferList = TimecardDC.MAS_EMPLOYEE_TRANSFER.Where(
employee => employee.HR_ADMIN_IND=="Y").Distinct().ToList();
回答by c0dem0nkey
Have try making it
尝试制作
.Distinct().ToList();
You can refer here LINQ: Distinct values
您可以在此处参考LINQ:不同的值
回答by lparry
There is a distinct method in linq which should do the trick.
linq 中有一个独特的方法应该可以解决问题。
回答by Kiran Solkar
Get Distinct using GroupBy
使用 GroupBy 区分
objEmployeeTransferList.GroupBy(x => x.empId).Select(g => g.First()).ToList();