Xav Sc
Xav Sc

Reputation: 641

refactoring complex linq query

I have created this linq pipeline that returns a dto that suits my needs perfectly. But as I try to keep my code clean I don't see how someone else's than me could easily read it. Any clean way I could do that?

    public static IEnumerable<SubscriptionOfferList> GetSubscriptionOffers(this IEnumerable<Product> products, IEnumerable<Plan> plans) =>
         products
             .GroupBy(p => p.Metadata["SubscriptionType"])
             .Select(productGroup => new SubscriptionOfferList
             {
                Name = productGroup.Key,
                Offers = productGroup.Select(p => new SubscriptionOffer
                {
                    Id = p.Id,
                    Name = p.Name,
                    Price = p.Metadata["SubscriptionPrice"],
                    Plans = plans
                    .Where(plan => plan.ProductId == p.Id)
                    .Select(plan => new PaymentPlan
                    {
                        Name = plan.Nickname,
                        Id = plan.Id,
                        Price = plan.Tiers ?? new List<PlanTier>
                        {
                            new PlanTier
                            {
                                UnitAmount = plan.Amount.GetValueOrDefault(),
                                UpTo = null
                            }
                        },
                    }).ToList()
                }).ToList(),
             });

Upvotes: 0

Views: 165

Answers (2)

BurnsBA
BurnsBA

Reputation: 4929

I'm not sure why comments are bad, but just break it up into smaller functions

private static List<Plan> MakePlans(IEnumerable<Plan> plans, int pid)
{
    return plans
            .Where(plan => plan.ProductId == pid)
            .Select(plan => new PaymentPlan
            {
                Name = plan.Nickname,
                Id = plan.Id,
                Price = plan.Tiers ?? new List<PlanTier>
                {
                    new PlanTier
                    {
                        UnitAmount = plan.Amount.GetValueOrDefault(),
                        UpTo = null
                    }
                },
            }).ToList();
}

public static IEnumerable<SubscriptionOfferList> GetSubscriptionOffers(this IEnumerable<Product> products, IEnumerable<Plan> plans) =>
     products
         .GroupBy(p => p.Metadata["SubscriptionType"])
         .Select(productGroup => new SubscriptionOfferList
         {
            Name = productGroup.Key,
            Offers = productGroup.Select(p => new SubscriptionOffer
            {
                Id = p.Id,
                Name = p.Name,
                Price = p.Metadata["SubscriptionPrice"],
                Plans = MakePlans(plans, p.Id)
            }).ToList(),
         });

Upvotes: 3

elecstrat
elecstrat

Reputation: 52

Put a comment on top of the method and some comments throughout the linq parts what they are doing, that should be fine as long as query works well.

Upvotes: 1

Related Questions