【问题标题】:Prevent repetition防止重复
【发布时间】:2018-07-25 12:10:37
【问题描述】:

select,orderby,take子句和toArray()重复,只有第一行不同。如何缩短这 3 个 linq 查询以避免重复?

我已经包含了用于返回模型视图的方法

    public async Task<ActionResult> GetPopularContent(int Records, int? WorkareaRef, string Jurisdiction)
    {
        var model = new PopularContentModel();
        int? languageId = null;

        var login = new Login(HttpContext);
        var branding = new DynamicBranding(login);

        if (branding.BrandingDefaultLanguage != Cam.Service.ContentService.defaultLanguageId)
        {
            languageId = branding.BrandingDefaultLanguage;
        }

        languageId = languageId ?? ContentService.defaultLanguageId;
        var jurisdictionRef = string.IsNullOrEmpty(Jurisdiction) ? (int?)null : metaDataService.GetJurisdictionRefViaName(Jurisdiction);

        var reads24 = (await mostPopularContentService.MostPopularLast24HoursAsync(languageId.Value, WorkareaRef, jurisdictionRef, Records + 30)).ToDictionary(x=>x.Article, x=>x.Total);
        var reads7 = (await mostPopularContentService.MostPopularThisWeekAsync(languageId.Value, Records + 30)).ToDictionary(x => x.Article, x => x.Total);
        var readsShared = (await mostPopularContentService.MostSharedThisWeekAsync(languageId.Value, Records + 30)).ToDictionary(x => x.Article, x => x.Total);

        model.Read7DayDataSource = articleService.GetArticlesFromGuidList(reads7.Select(x=>x.Key), wcagOnly: branding.StrictlyWCAG2Accessible, useCache: true)
            .Select(x => new PopularArticle() { Article = x, ArticleGuid = x.ArticleGUID })
            .Where(x=> contentAdminTestingDataFilterService.AllowFirmAccess(x.Article.FirmRef, lexUser) && x.Article.RemoveDate == null)
            .OrderByDescending(x=> reads7[x.ArticleGuid])
            .Take(Records)
            .ToArray();
        model.Read24DataSource = articleService.GetArticlesFromGuidList(reads24.Select(x => x.Key), wcagOnly: branding.StrictlyWCAG2Accessible, useCache: true)
            .Select(x => new PopularArticle() { Article = x, ArticleGuid = x.ArticleGUID })
            .Where(x => contentAdminTestingDataFilterService.AllowFirmAccess(x.Article.FirmRef, lexUser) && x.Article.RemoveDate == null)
            .OrderByDescending(x => reads24[x.ArticleGuid])
            .Take(Records)
            .ToArray();
        model.SharedDataSource = articleService.GetArticlesFromGuidList(readsShared.Select(x => x.Key), wcagOnly: branding.StrictlyWCAG2Accessible, useCache: true)
            .Select(x => new PopularArticle() { Article = x, ArticleGuid = x.ArticleGUID })
            .Where(x => contentAdminTestingDataFilterService.AllowFirmAccess(x.Article.FirmRef, lexUser) && x.Article.RemoveDate == null)
            .OrderByDescending(x => readsShared[x.ArticleGuid])
            .Take(Records)
            .ToArray();

        model.LastUpdated = DateTime.Now.AddDays(-1).Date;

        DecorateFirmName(model);

        model.ShowSocialButtons = branding.ShowSocialButtons;

        return PartialView("PopularContentPartial", model);
    }

谢谢

【问题讨论】:

  • 您将创建一个方法,该方法接受适当的集合或查询参数,并返回适当的数组,然后重用它。如果没有有关所涉及类型的更多信息,我无法提供更具体的评论。

标签: c# linq


【解决方案1】:

您创建一个将reads7reads24readsShared 作为参数类型并返回结果的方法:

IEnumerable<PopularArticle> Get(Dictionary<Guid, SOMETHING> variable)
{
    return articleService.GetArticlesFromGuidList(variable.Select(x => x.Key), wcagOnly: branding.StrictlyWCAG2Accessible, useCache: true)
        .Select(x => new PopularArticle() { Article = x, ArticleGuid = x.ArticleGUID })
        .Where(x => contentAdminTestingDataFilterService.AllowFirmAccess(x.Article.FirmRef, lexUser) && x.Article.RemoveDate != null)
        .OrderByDescending(x => variable[x.ArticleGuid])
        .Take(Records)
        .ToArray();
}

调用示例:

model.Read7DayDataSource = this.Get(reads7);

【讨论】:

  • 方法或调用者中缺少.ToArray() 调用
猜你喜欢
  • 2010-11-09
  • 2011-04-23
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 2020-08-12
  • 2016-09-28
  • 2015-02-01
相关资源
最近更新 更多