Jon Harding
Jon Harding

Reputation: 4946

How remove duplicate item by a property value in a List<>

Can someone help me take this Linq query and show me how to pull out the Distinct product Names

List<StatePlan> planTypes = (from ppt in context.PlanPlanTypes
     join pt in context.PlanTypes on ppt.PlanTypeRowID equals pt.PlanTypeRowID
     join ppts in context.PlanPlanTypeStates on ppt.PlanPlanTypeRowID equals ppts.PlanPlanTypeRowID
     join p in context.Plans on ppt.PlanRowID equals p.PlanRowID
     where ppts.StateCode == stateCode
     where p.IsActive == true
     select new StatePlan
     {   
         PlanID = p.PlanRowID,
         StateCode = stateCode,
         Name = p.Name,
         Description = p.Description,
         Disclaimer = p.Disclaimer,
         Sequence = p.DisplaySequence,
         //Rates = GetRates(p.PlanRowID),
         //Types = GetPlanTypes(p.PlanRowID, stateCode)
     }).ToList();
return planTypes;

Upvotes: 0

Views: 2798

Answers (5)

Abdul Munim
Abdul Munim

Reputation: 19217

Group it first then take the first

List<StatePlan> planTypes = (from ppt in context.PlanPlanTypes
                                             join pt in context.PlanTypes on ppt.PlanTypeRowID equals pt.PlanTypeRowID
                                             join ppts in context.PlanPlanTypeStates on ppt.PlanPlanTypeRowID equals ppts.PlanPlanTypeRowID
                                             join p in context.Plans on ppt.PlanRowID equals p.PlanRowID
                                             where ppts.StateCode == stateCode
                                             where p.IsActive == true
                                             select new StatePlan
                                             {   
                                                 PlanID = p.PlanRowID,
                                                 StateCode = stateCode,
                                                 Name = p.Name,
                                                 Description = p.Description,
                                                 Disclaimer = p.Disclaimer,
                                                 Sequence = p.DisplaySequence,
                                                 //Rates = GetRates(p.PlanRowID),
                                                 //Types = GetPlanTypes(p.PlanRowID, stateCode)
                                             })
                                             .GroupBy(g => g.Name)
                                             .Select(s => s.First())
                                             .ToList();
            return planTypes;

Upvotes: 1

Guffa
Guffa

Reputation: 700162

Assuming that the product name is the Name property in the planTypes list, and that it is a string:

List<string> productNames = planTypes.Select(t => t.Name).Distinct().ToList();

Upvotes: 0

cederlof
cederlof

Reputation: 7383

planTypes.Select(pt=>pt.Name).Distinct();

Upvotes: 1

MBen
MBen

Reputation: 3996

You can Call Distinct and provide and IEqualityComparer for StatePlan

Distinct

Upvotes: 0

CodesInChaos
CodesInChaos

Reputation: 108790

I'm not sure if I understood the question correctly, but if you want items that have a certain distinct field, you can use groupby+first:

seq.GroupBy(item=>item.Name).Select(group=>group.First())

You could also use a projection equality comparer, but I consider that a bit ugly. For me using Distinct implies that you don't care which of the equivalent items you get. Whereas groupby+first explicitly states that you want the first one.

GroupBy also allows you to collect additional information in your Select clause, for example how many items with such a name exist.


If you only want the distinct names without the associated item, combine select and distinct:

seq.Select(item=>item.Name).Distinct()

Upvotes: 3

Related Questions