Thursday, January 20, 2011

Linq Spagetti

 //Property Options
            IDictionaryAdvertPropertyint> advertPropertyGroupList =
    (from r in resultPropertyOption
     where r.AdvertPropertyID != null && r.AdvertPropertyOptionID != null && r.OptionCount != 0
     group r by r.AdvertPropertyID into g
     select
        new
        {
            C = new EmlakMilliyet.Service.Model.AdvertSearch.AdvertProperty
            {
                Id = (int)g.Key.Value,
                Name = this.AdvertLookupDataService.GetAdvertProperty((int)g.Key.Value).Name,
                Options =
                    (from v in g
                     select
                        new
                        {
                            L = this.AdvertLookupDataService.GetAdvertPropertyOption((int)g.Key.Value, (int)v.AdvertPropertyOptionID) ?? new AdvertPropertyOption
                            {
                                Id = 0,
                                Name = "",
                                SearchOptionOrder = 0
                            },
                            TotalCount = g.Where(w => w.AdvertPropertyOptionID == v.AdvertPropertyOptionID).Sum(a => a.OptionCount.GetValueOrDefault())
                        })
                     .OrderBy(o => o.L.Name)
                     .ToDictionary(p => p.L, p => p.TotalCount)
            },
            TotalCount = g.Sum(a => a.OptionCount.GetValueOrDefault())
        }).OrderBy(o => o.C.Name).ToDictionary(p => p.C, p => p.TotalCount);

2 comments:

Anonymous said...

I think one of your advertisements caused my internet browser to resize, you might want to put that on your blacklist.

Peet Someone said...

Oh, yes, Linq can cause unreadable spagetti. I think it's a worldwide-common problem.