gpt4 book ai didi

c# - LINQ 查询以选择与成对数组匹配的行

转载 作者:行者123 更新时间:2023-11-30 12:47:39 26 4
gpt4 key购买 nike

现在,我有一个名为 TrainingPlan 的类,如下所示:

public class TrainingPlan
{
public int WorkgroupId { get; set; }
public int AreaId { get; set; }
}

我得到了这些实例的数组,需要从数据库中加载匹配的训练计划。 WorkgroupIdAreaId 基本上构成了一个复合键。我现在正在做的是像这样遍历每个 TrainingPlan:

foreach (TrainingPlan plan in plans)
LoadPlan(pid, plan.AreaId, plan.WorkgroupId);

然后,LoadPlan 有一个 LINQ 查询来加载单个计划:

var q = from tp in context.TPM_TRAININGPLAN.Include("TPM_TRAININGPLANSOLUTIONS")
where tp.PROJECTID == pid && tp.AREAID == areaid &&
tp.WORKGROUPID == workgroupid
select tp;

return q.FirstOrDefault();

问题:

这可行,但是对于大量计划来说速度非常慢。我相信,如果我可以执行单个 LINQ 查询以同时加载每个 TPM_TRAININGPLAN,这会快得多。

我的问题:

给定一组 TrainingPlan 对象,我如何才能一次加载每个匹配的 WorkgroupId/AreaId 组合?此查询应转换为类似的 SQL 语法:

SELECT * FROM TPM_TRAININGPLANS
WHERE (AREAID, WORKGROUPID) IN ((1, 2), (3, 4), (5, 6), (7, 8));

最佳答案

我使用 Contains 来运行类似于 where-in 的批量过滤器。我设置了您的场景的粗略近似值。单个选择查询实际上比 Contains 运行得更快。我建议您在绑定(bind)数据库的情况下运行类似的测试,看看您的结果如何结束。理想情况下也看看它是如何扩展的。我在 visual studio 2012 中运行 .NET 4.0。我卡在 ToList() 调用中以解决潜在的延迟加载问题。

public class TrainingPlan
{
public int WorkgroupId { get; set; }
public int AreaId { get; set; }

public TrainingPlan(int workGroupId, int areaId)
{
WorkgroupId = workGroupId;
AreaId = areaId;
}
}

public class TrainingPlanComparer : IEqualityComparer<TrainingPlan>
{
public bool Equals(TrainingPlan x, TrainingPlan y)
{
//Check whether the compared objects reference the same data.
if (x.WorkgroupId == y.WorkgroupId && x.AreaId == y.AreaId)
return true;

return false;
}

public int GetHashCode(TrainingPlan trainingPlan)
{
if (ReferenceEquals(trainingPlan, null))
return 0;

int wgHash = trainingPlan.WorkgroupId.GetHashCode();
int aHash = trainingPlan.AreaId.GetHashCode();

return wgHash ^ aHash;
}
}


internal class Class1
{
private static void Main()
{
var plans = new List<TrainingPlan>
{
new TrainingPlan(1, 2),
new TrainingPlan(1, 3),
new TrainingPlan(2, 1),
new TrainingPlan(2, 2)
};

var filter = new List<TrainingPlan>
{
new TrainingPlan(1, 2),
new TrainingPlan(1, 3),
};

Stopwatch resultTimer1 = new Stopwatch();
resultTimer1.Start();
var results = plans.Where(plan => filter.Contains(plan, new TrainingPlanComparer())).ToList();
resultTimer1.Stop();

Console.WriteLine("Elapsed Time for filtered result {0}", resultTimer1.Elapsed);

Console.WriteLine("Result count: {0}",results.Count());

foreach (var item in results)
{
Console.WriteLine("WorkGroup: {0}, Area: {1}",item.WorkgroupId, item.AreaId);
}

resultTimer1.Reset();

resultTimer1.Start();
var result1 = plans.Where(p => p.AreaId == filter[0].AreaId && p.WorkgroupId == filter[0].WorkgroupId).ToList();
var result2 = plans.Where(p => p.AreaId == filter[1].AreaId && p.WorkgroupId == filter[1].WorkgroupId).ToList();
resultTimer1.Stop();

Console.WriteLine("Elapsed time for single query result: {0}",resultTimer1.Elapsed);//single query is faster

Console.ReadLine();
}
}

关于c# - LINQ 查询以选择与成对数组匹配的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16307765/

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