1

I have the Input format in following way

S.no Name wages

1       Tom     $200

1       Tom     $300

1       Tom     $400

2        Rob     $500

2        Rob     $600

Result set should be in the following way

  Name   OriginalWage  DuplicateWage

   Tom       $200              $300

   Tom      $200               $400

   Rob       $500               $600

I should leave the first record and take the duplicate records into count .Here the original wages is the wage of the first distinct record.

How can i accomplish the result set using 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))
Yoda
  • 319
  • 2
  • 5
  • 20
  • Well, what have you tried so far? And is this LINQ to Objects, LINQ to SQL, something else? Do you have an ordering column you can use to definitely find the "first" value? – Jon Skeet Jun 26 '13 at 05:56
  • Where's the duplicate and what have you.tried? Sounds like you need group – Sayse Jun 26 '13 at 05:56
  • @JonSkeet 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)) – Yoda Jun 26 '13 at 06:52

2 Answers2

1

You can make something like this

 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));
        }

code is not mine and has been taken from: Use LINQ to concatenate multiple rows into single row (CSV property)

this link might be helpful too

EDITED

Sorry, misunderstood you question something like this can do the trick

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 }
Community
  • 1
  • 1
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
1

I managed to get the result set you wanted (proved a nice challenge):

It is assuming that the first record it finds for an employee is the original wage however...

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
             };

A full LinqPad script is here for testing: http://share.linqpad.net/wgxcns.linq

Example Result

enter image description here

Samuel Parkinson
  • 2,992
  • 1
  • 27
  • 38