我有以下方式输入格式
S.no姓名工资
1 Tom $200
1 Tom $300
1 Tom $400
2 Rob $500
2 Rob $600
结果集应采用以下方式
Name OriginalWage DuplicateWage
Tom $200 $300
Tom $200 $400
Rob $500 $600
我应该留下第一张记录,并将重复的记录计算在内。原来的工资是第一份不同记录的工资。
如何使用linq完成结果集。
This is the one i tried so far
//Get duplicate values
Dim Duplicates = wageRecordList.GroupBy(Function(w) w.serialnumber).Where(Function(d) d.Count > 1)
//load duplicates to a list
lstDuplicateRecords=Duplicates
//Read list--This one is a hard coded sort of thing and works only for one set of duplicate values
lstResult = (From duplicateRecords In lstDuplicateRecords.Skip(1) Select serialnumber= duplicateRecords.serialnumber, Name= duplicateRecords.Name, OriginalWages= CType(lstDuplicateRecords(0).Wages, String), _
DuplicateWages = CType(duplicateRecords.wages, String))
答案 0 :(得分:1)
你可以做这样的事情
var groupedUsers = from user in users
group user by user.User into userGroup
select new
{
User = userGroup.Key,
userHobies =
userGroup.Aggregate((a, b) =>
new { User = a.User, Hobby = (a.Hobby + ", " + b.Hobby) }).Hobby
}
;
foreach (var x in groupedUsers)
{
Debug.WriteLine(String.Format("{0} {1}", x.User, x.userHobies));
}
代码不是我的,而且来自:Use LINQ to concatenate multiple rows into single row (CSV property)
此link可能也会有所帮助
<强> EDITED 强>
抱歉,误解了你的问题 像这样的东西可以做到这一点
var query = from sal in _yourcontext
join salmin in ( from sal1 in _yourcontext
group sal1 by sal1.name into group
select new{
Name = group.Key
MinSal = group.Min(sal1=>sal1.Salary))
}
on sal.Name equals salmin.Name into result
where sal.Salary != salmin.MinSal
select new{ salmin.Name,salmin.MinSal,sal.Salary }
答案 1 :(得分:1)
我设法得到你想要的结果集(证明是一个很好的挑战):
假设它为员工找到的第一条记录是原始工资,但是......
var result = from employeeWages in GetEmployees().GroupBy(e => e.Name)
from duplicateWage in employeeWages.Skip(1).Select(e => e.Wage)
select new
{
Name = employeeWages.Key,
OriginalWage = employeeWages.First().Wage,
DuplicateWage = duplicateWage
};
完整的LinqPad脚本用于测试:http://share.linqpad.net/wgxcns.linq
示例结果