How to update duplicate records in LIST<T> by group by using LINQ

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
i have a list which has some time duplicate line item and xfund code in records. so i want to run a linq query on my list which will find duplicate records based on line item and xfund code and update xfundcode .

suppose if there are three records like

lineitem xfundcode
--------- ----------
R&D RD
SG&A SGA

R&D RD
SG&A SGA

R&D RD
SG&A SGA

in above sample there are 6 records and 4 records has duplicate line item and xfundcode. now i want linq query will leave first 2 records as it is and update next 4 records xfundcode to empty value. i tried below query but it update all the xfundcode.



if (lstMainData.Count > 0)
{
lstMainData = lstMainData.DistinctBy(x => new { li = x.LineItem, xc = x.XFundCode }).ToList();

lstMainData = lstMainData
.GroupBy(x => new { li = x.LineItem, xc = x.XFundCode })
.SelectMany(g => g)
.ToList();
lstMainData.ForEach(x => x.XFundCode = string.Empty);
}
please help me with right linq query. thanks

Continue reading...
 
Back
Top