Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 防止重复_C#_Linq - Fatal编程技术网

C# 防止重复

C# 防止重复,c#,linq,C#,Linq,select、where orderby、take子句和toArray()重复,只有第一行不同。如何缩短这3个linq查询以避免重复 我已经包括了用于返回带有模型的视图的方法 public async Task<ActionResult> GetPopularContent(int Records, int? WorkareaRef, string Jurisdiction) { var model = new PopularContentModel(

select、where 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);
    }
公共异步任务GetPopularContent(int记录,int?WorkareaRef,字符串辖区)
{
var model=新的PopularContentModel();
int?languageId=null;
var login=新登录(HttpContext);
var品牌=新的动态品牌(登录);
if(branding.BrandingDefaultLanguage!=Cam.Service.ContentService.defaultLanguageId)
{
languageId=branding.BrandingDefaultLanguage;
}
languageId=languageId??ContentService.defaultLanguageId;
var justictionref=string.IsNullOrEmpty(辖区)?(int?)null:metaDataService.GetJustictionRefViaName(辖区);
var reads24=(等待mostPopularContentService.mostpopularlast24hourssync(languageId.Value,WorkareaRef,jurisdictionRef,Records+30)).ToDictionary(x=>x.Article,x=>x.Total);
var reads7=(等待mostPopularContentService.MostPopularThisWeekAsync(languageId.Value,Records+30)).ToDictionary(x=>x.Article,x=>x.Total);
var readsShared=(等待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=>newpopularticle(){Article=x,ArticleGuid=x.ArticleGuid})
.Where(x=>contentAdminTestingDataFilterService.AllowFirmAccess(x.Article.FirmRef,lexUser)和&x.Article.RemoveDate==null)
.OrderByDescending(x=>reads7[x.ArticleGuid])
.Take(记录)
.ToArray();
model.Read24DataSource=articleService.GetArticlesFromGuidList(reads24.Select(x=>x.Key),wcagOnly:branding.StrictlyWCAG2Accessible,useCache:true)
.Select(x=>newpopularticle(){Article=x,ArticleGuid=x.ArticleGuid})
.Where(x=>contentAdminTestingDataFilterService.AllowFirmAccess(x.Article.FirmRef,lexUser)和&x.Article.RemoveDate==null)
.OrderByDescending(x=>reads24[x.ArticleGuid])
.Take(记录)
.ToArray();
model.SharedDataSource=articleService.GetArticlesFromGuidList(readsShared.Select(x=>x.Key),wcagOnly:branding.Striclywcag2accessible,useCache:true)
.Select(x=>newpopularticle(){Article=x,ArticleGuid=x.ArticleGuid})
.Where(x=>contentAdminTestingDataFilterService.AllowFirmAccess(x.Article.FirmRef,lexUser)和&x.Article.RemoveDate==null)
.OrderByDescending(x=>readsShared[x.ArticleGuid])
.Take(记录)
.ToArray();
model.LastUpdated=DateTime.Now.AddDays(-1).Date;
decorefirmName(模型);
model.ShowSocialButtons=branding.ShowSocialButtons;
返回PartialView(“PopularContentPartial”,模型);
}

感谢您创建了一个将
reads7
reads24
readsShared
作为参数类型并返回结果的方法:

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

您将创建一个方法,该方法接收适当的集合或查询参数,并返回适当的数组,然后重用它。如果没有有关所涉及类型的更多信息,我无法提供更具体的注释。
.ToArray()
调用在方法或调用方中丢失
model.Read7DayDataSource = this.Get(reads7);