Cameron Albert

Ramblings of software development, games and technology.

LINQ Queries

I have spent the last three days writing LINQ qeueries over object collections and have to come to rely on LINQ pretty heavily for most query operations. I have played around some with LINQ to SQL and LINQ to Entities but I mostly use it to just query over some good old fashion collections like so:

 

var groups = proposal.Lines.Where(kvp => kvp.Key.Equals(key.Quarter))
                                           .Select(kvp => kvp.Value.Where(l => l.AvailType.Spot == "N" && l.AvailType.Type == "RE"))
                                           .FirstOrDefault()
                                           .GroupBy(l => new { l.NetworkId, l.UnitLength, l.BrandId });

            List<TimePeriod> weeks = proposal.GetWeeksFromOrderFlightDates(key.Quarter);
            foreach (var group in groups)
            {
                BudgetRow row = AddBudgetRow(budgetDefinition,
                    new List<string>(new string[] { group.Key.NetworkId.ToString(), group.Key.UnitLength.ToString(), group.Key.BrandId.ToString() }),
                    budgetRows, BudgetDefinitionType.WeeklyNetworkAvailTypeImps);

                foreach (var week in weeks)
                {
                    var units = group.SelectMany(l => l.Units.Where(u => week.contains(u.AirDate)));

                    row.setRawValue(week, units.Sum(u => (decimal?)u.Demo1Imps));

                    row.setLockedValue(week, units.Where(u => u.IsLockedOutOfUnitAllocation).Sum(u => (decimal?)u.Demo1Imps));
                }
            }

About these ads

Written by Cameron

May 7, 2009 at 6:09 pm

Posted in Uncategorized

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: