Click here to Skip to main content
15,938,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends i need to group a list of objects based on a field Cusip . and need to return new list which has count>1

Please help

public class AccruedClass
            public string Cusip { get; set; }
            public string Symbol { get; set; }
            public double AccruedInterest { get; set; }
            public double PriceFactor { get; set; }
            public double Quantity { get; set; }            
            public double CalcAcc { get { return AccruedInterest * PriceFactor * Quantity; } }

List<AccruedClass> ac = new List<AccruedClass>();
ac = validaccrued();
ac.ForEach(item => Console.Write(item.Cusip + "," + item.AccruedInterest + "," + item.Quantity + "\n"));

1 solution

Grouping is easy:
var grouped = ac.GroupBy(a => a.Cusip);
But you want only those groups with more than one member?
Try this:
List<AccruedClass> grouped = ac.GroupBy(a => a.Cusip).Where(g => g.Count() > 1).SelectMany(g => g.ToList()).ToList();

[edit]Forgot to encode HTML tag characters...[/edit]
Share this answer
jinesh sam 18-Jul-15 4:41am    
Thanks :)
OriginalGriff 18-Jul-15 4:54am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900