gpt4 book ai didi

c# - 使 Linq 查询并行运行

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:12 24 4
gpt4 key购买 nike

我可能有 40 个 linq 查询,我想并行执行它们。查询之间没有依赖关系,因此并行运行每个查询应该没问题。我想知道如何使下面的代码“更快”/“更好”。

我的两个基本查询和联合:

public override void Filter()
{
var general = (from z in ctx.Interactions
where z.ActivityDate <= EndDate && z.ActivityDate >= StartDate &&
z.Indepth == false && z.Type == InteractionTypes.General
select new { Entries = z.EntryCount }).Sum(x => x.Entries).GetValueOrDefault();

var Indepth = (from z in ctx.Interactions
join program in ctx.Programs on z.Program.Id equals program.Id
where z.ActivityDate <= EndDate && z.ActivityDate >= StartDate && z.Indepth == true && program.Reportable
select z).Count();

...Remaining 38 queries....

AddQuarterlyInfo("# of General Inquiries", "1.1", general);
AddQuarterlyInfo("# of In-Depth Counselling and Information Services Interviews", "1.2", Indepth);
...Union remaining 38 queries...
}

最佳答案

如果您使用 Entity Framework 6,则可以通过执行以下操作来使用异步查询:

var blogs = (from b in db.Blogs 
orderby b.Name
select b).ToListAsync();

var blogs2 = (from b in db.Blogs
orderby b.Name
select b).ToListAsync();

var result = await Task.WhenAll(new [] {blogs, blogs2}));

//this is reached when all queries are completed.

关于c# - 使 Linq 查询并行运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29215534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com