C# 如何更快地执行这个System.Linq语句?

C# 如何更快地执行这个System.Linq语句?,c#,performance,linq,C#,Performance,Linq,我有一个自定义类的可枚举项,它是我的子集,如下所示: var sourceSet = await GetSourceSetData(inputParam1, inputParam2); _logger.Info.Write(message: "CheckpointA"); var resultSet = sourceSet.Where(x => (x.Property1 + x.Property2 > 0) ||

我有一个自定义类的可枚举项,它是我的子集,如下所示:

var sourceSet = await GetSourceSetData(inputParam1, inputParam2);

_logger.Info.Write(message: "CheckpointA");

var resultSet = sourceSet.Where(x => (x.Property1 + x.Property2 > 0) ||
                                     (!myBoolean && x.Property3.SubProperty > x.Property4));

_logger.Info.Write(message: "CheckpointB");
GetSourceData由几个db调用组成

var dataTask1 = _db.GetThisData(inputList);
var dataTableForTVP = GetCutoffDates(inputList, myBoolean).CopyToDataTable();
var dataTask2 = _db.GetThatData(dataTableForTVP, myBoolean);
var dataTask3 = _db.GetMoreData(inputList, myBoolean);
var dataTask4 = _db.GetEvenMoreData(inputList);
var dataTask5 = _db.GetMoarMoreData(inputList);

var task1Result = await dataTask1;
var task2Result = await dataTask2;
var task3Result = await dataTask3;
var task4Result = await dataTask4;
var task5Result = await dataTask5;

var sourceSuperSet = inputList.Select(x => new MyCustomClass()
{
    IdProperty = x.IntProperty,
    CategoryProperty = 0,
    PropertyY = task1Result.FirstOrDefault(y => y.IdProperty == x.IntProperty) == null ? 0.00m : task1Result.FirstOrDefault(y => y.IdProperty == x.IntProperty).DecimalProperty,
    Property3 = task2Result.FirstOrDefault(z => z.IdProperty == x.IntProperty) ?? new FourPropertyClass() {IdProperty = x.IntProperty, DecProperty1 = 0, DecProperty2 = 0},
    PropertyU = task3Result.FirstOrDefault(u => u.IdProperty == x.IntProperty) == null ? 0.00m : task3Result.FirstOrDefault(u => u.IdProperty == x.IntProperty).DecimalProperty,
    PropertyV = task4Result.FirstOrDefault(v => v.IdProperty == x.IntProperty) == null ? 0.00m : task4Result.FirstOrDefault(v => v.IdProperty == x.IntProperty).DecimalProperty,
    PropertyW = task5Result.FirstOrDefault(w => w.IdProperty == x.IntProperty) == null ? 0.00m : task5Result.FirstOrDefault(w => w.IdProperty == x.IntProperty).DecimalProperty
});

return sourceSuperSet.Where(x => x.Property3.DecPropertyCalc > 0 &&
                                (x.Property3.DecProprtyCalc - x.PropertyY > 0.0m || 
                                 x.PropertyW - x.PropertyV > 0.0m)
                           );
从检查点a到检查点B只需要花费很长的时间(比如12分钟),在可枚举项中只有几千项。有什么更好的方法可以得到我想要的结果?提前感谢。

您可以使用LINQ方法创建辅助数据的查找:

var task1Lookup = task1Result.ToLookup(x => x.IdProperty);
var task2Lookup = task2Result.ToLookup(x => x.IdProperty);
var task3Lookup = task3Result.ToLookup(x => x.IdProperty);
var task4Lookup = task4Result.ToLookup(x => x.IdProperty);
var task5Lookup = task5Result.ToLookup(x => x.IdProperty);

var sourceSuperSet = inputList.Select(x => new MyCustomClass()
{
    IdProperty = x.IntProperty,
    CategoryProperty = 0,
    PropertyY = (task1Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
    Property3 = (task2Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
    PropertyU = (task3Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
    PropertyV = (task4Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
    PropertyW = (task5Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
});
您可以使用LINQ方法创建辅助数据的查找:

var task1Lookup = task1Result.ToLookup(x => x.IdProperty);
var task2Lookup = task2Result.ToLookup(x => x.IdProperty);
var task3Lookup = task3Result.ToLookup(x => x.IdProperty);
var task4Lookup = task4Result.ToLookup(x => x.IdProperty);
var task5Lookup = task5Result.ToLookup(x => x.IdProperty);

var sourceSuperSet = inputList.Select(x => new MyCustomClass()
{
    IdProperty = x.IntProperty,
    CategoryProperty = 0,
    PropertyY = (task1Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
    Property3 = (task2Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
    PropertyU = (task3Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
    PropertyV = (task4Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
    PropertyW = (task5Lookup[x.IntProperty].FirstOrDefault()?.DecimalProperty).GetValueOrDefault(0.00m),
});

这是在和数据库说话吗?在对其执行Linq操作之前,您能将其具体化为一个列表吗?是的,这也是我的第一个想法。
sourceSet
是什么类型?您的属性是否在get{}部分执行任何操作?请回答这个问题,以显示使用什么语句将“数据”分配给
sourceSet
。换句话说:
sourceSet=
右侧是什么?如果作业使用了其他来源,也要显示这些作业。如果我们能看到
MyCustomClass
的定义,这也会有所帮助。我不是在问类型。属性1是否只是返回一个值,或者是否进行了一些计算或其他操作。没有其他理由说明你的代码运行缓慢。当它运行时,点击“全部中断”调试器,查看它在哪里停止。这是在与数据库对话吗?在对其执行Linq操作之前,您能将其具体化为一个列表吗?是的,这也是我的第一个想法。
sourceSet
是什么类型?您的属性是否在get{}部分执行任何操作?请回答这个问题,以显示使用什么语句将“数据”分配给
sourceSet
。换句话说:
sourceSet=
右侧是什么?如果作业使用了其他来源,也要显示这些作业。如果我们能看到
MyCustomClass
的定义,这也会有所帮助。我不是在问类型。属性1是否只是返回一个值,或者是否进行了一些计算或其他操作。没有其他理由说明你的代码运行缓慢。当它运行时,点击“全部中断”调试器并查看它在哪里停止。