1

我有以下方式的输入格式

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))
4

2 回答 2

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)

这个链接也可能有帮助

已编辑

抱歉,误解了你的问题,这样的事情可以解决问题

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 }
于 2013-06-26T06:04:22.887 回答
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

示例结果

在此处输入图像描述

于 2013-06-27T15:20:16.510 回答