LINQ Queries

by Cameron Albert 7. May 2009 18:09

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));
                }
            }

Tags: ,

Powered by BlogEngine.NET 1.5.0.7
Modified Theme by Mads Kristensen

About the Author

CameronAlbert.com I am Senior Software Development Consultant specializing in Silverlight, WPF and the Microsoft .NET Framework. 

I have released an iPhone game called the Adventures of Puppyman that was built using ExEn and am currently working on a WP7 and iPhone version of Perenthia soon to be released.

View Cameron Albert's profile on LinkedIn
See how we're connected

Follow cameronalbert on Twitter

 

Recommended Books

Silverlight 4 Business Application Development - Beginner's Guide:

http://www.packtpub.com/microsoft-silverlight-4-business-application-development-beginners-guide/book

Microsoft Silverlight 4 Business Application Development: Beginner’s Guide