Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 消除.net lambda表达式_C#_Linq_Lambda - Fatal编程技术网

C# 消除.net lambda表达式

C# 消除.net lambda表达式,c#,linq,lambda,C#,Linq,Lambda,我正在评估C#.NET web应用程序中的一个性能问题,并将瓶颈追溯到一个链接的lambda表达式。我想完全删除lambda表达式,这样我可以评估链中每个步骤的性能,但是我对lambda表达式比较陌生。有没有人想过如何将第二个lambda express重构为更传统的代码,以便跟踪每个步骤或操作 IEnumerable<OurPerformance> validPerformances = package.TimeFilteredValidPerformances(visitDate

我正在评估C#.NET web应用程序中的一个性能问题,并将瓶颈追溯到一个链接的lambda表达式。我想完全删除lambda表达式,这样我可以评估链中每个步骤的性能,但是我对lambda表达式比较陌生。有没有人想过如何将第二个lambda express重构为更传统的代码,以便跟踪每个步骤或操作

IEnumerable<OurPerformance> validPerformances = package.TimeFilteredValidPerformances(visitDateAndTime);

IEnumerable<WebPerformance> webPerformances = performanceGroup.RegularNonPassedPerformances
            .Where(performance => validPerformances.Select(perf => perf.PerformanceId).Contains(performance.PerformanceId))                
            .Select(performance =>
                new WebPerformance
                {
                    Date = performance.PerformanceDate.ToJavaScriptDateString(),
                    PerformanceId = performance.PerformanceId,
                    Title = performance.UserFriendlyTitle,
                    ProductionSeasonId = performance.ProductionSeasonId,
                    AvailableCount = performance.AvailableCount
                });

**IEnumerable<WebProduction> webProductions = webPerformances
            .GroupBy(performance => performance.ProductionSeasonId)
            .ToDictionary(grouping => SheddProduction.GetOurProduction(grouping.Key), grouping => grouping.ToList())
            .Select(perfsByProduction =>
                new WebProduction
                {
                    ProductionSeasonId = perfsByProduction.Key.ProductionSeasonNumber,
                    Duration = perfsByProduction.Key.Duration,
                    Title = perfsByProduction.Key.UserFriendlyTitle,
                    Synopsis = perfsByProduction.Key.UserFriendlySynopsis,
                    ThumbnailImage = perfsByProduction.Key.PreviewImage,
                    Performances = perfsByProduction.Value
                });**
IEnumerable validPerformances=package.TimeFilteredValidPerformances(visitDate和Time);
IEnumerable WebPerformance=性能组。常规非许可性能
.Where(performance=>validPerformances.Select(perf=>perf.PerformanceId).Contains(performance.PerformanceId))
.选择(性能=>
新网络性能
{
日期=performance.PerformanceDate.ToJavaScriptDateString(),
PerformanceId=performance.PerformanceId,
Title=performance.UserFriendlyTitle,
ProductionSeasonId=性能。ProductionSeasonId,
AvailableCount=性能。AvailableCount
});
**IEnumerable webProductions=WebPerformance
.GroupBy(性能=>performance.ProductionSeasonId)
.ToDictionary(grouping=>SheddProduction.GetOurProduction(grouping.Key),grouping=>grouping.ToList())
.选择(perfsByProduction=>
新网络制作
{
ProductionSeasonId=perfsByProduction.Key.ProductionSeasonNumber,
持续时间=perfsByProduction.Key.Duration,
Title=perfsByProduction.Key.UserFriendlyTitle,
Synopsis=perfsByProduction.Key.UserFriendlySynopsis,
ThumbnailImage=perfsByProduction.Key.PreviewImage,
性能=perfsByProduction.Value
});**

我不能建议任何排除lambda表达式的算法,但我有另一个建议。尝试将列表用于WebPerformance,而不是IEnumerable集合。
调用ToList()以获取列表。在这种情况下,第二个lambda表达式将与list一起使用,并且不会重新计算IEnumerable集合。

实际上相当直接地将其分解为更小的片段,这些片段更明显地转换为“传统”代码。只需将每个linq表达式结果存储在一个局部变量中,如下所示:

var groupedWebPerformances = webPerformances.GroupBy(performance => performance.ProductionSeasonId);
var webPerformancesDictionary = groupedWebPerformances .ToDictionary(grouping => SheddProduction.GetOurProduction(grouping.Key), grouping => grouping.ToList());
IEnumerable<WebProduction> webProductions = webPerformancesDictionary.Select(perfsByProduction =>
                new WebProduction
                {
                    ProductionSeasonId = perfsByProduction.Key.ProductionSeasonNumber,
                    Duration = perfsByProduction.Key.Duration,
                    Title = perfsByProduction.Key.UserFriendlyTitle,
                    Synopsis = perfsByProduction.Key.UserFriendlySynopsis,
                    ThumbnailImage = perfsByProduction.Key.PreviewImage,
                    Performances = perfsByProduction.Value
                });
确保在创建所有可枚举项时,通过对它们执行
ToList
,强制它们一次性求值,如下所示:

var groupedWebPerformances = webPerformances.GroupBy(performance => performance.ProductionSeasonId);
var webPerformancesDictionary = groupedWebPerformances .ToDictionary(grouping => SheddProduction.GetOurProduction(grouping.Key), grouping => grouping.ToList());
IEnumerable<WebProduction> webProductions = webPerformancesDictionary.Select(perfsByProduction =>
                new WebProduction
                {
                    ProductionSeasonId = perfsByProduction.Key.ProductionSeasonNumber,
                    Duration = perfsByProduction.Key.Duration,
                    Title = perfsByProduction.Key.UserFriendlyTitle,
                    Synopsis = perfsByProduction.Key.UserFriendlySynopsis,
                    ThumbnailImage = perfsByProduction.Key.PreviewImage,
                    Performances = perfsByProduction.Value
                });
List validPerformances=package.TimeFilteredValidPerformances(visitDateAndTime.ToList();
列出Web性能=性能组。常规非许可性能
.Where(performance=>validPerformances.Select(perf=>perf.PerformanceId).Contains(performance.PerformanceId))
.选择(性能=>
新网络性能
{
日期=performance.PerformanceDate.ToJavaScriptDateString(),
PerformanceId=performance.PerformanceId,
Title=performance.UserFriendlyTitle,
ProductionSeasonId=性能。ProductionSeasonId,
AvailableCount=性能。AvailableCount
})
.ToList();
列出webProductions=WebPerformance
.GroupBy(性能=>performance.ProductionSeasonId)
.ToDictionary(grouping=>SheddProduction.GetOurProduction(grouping.Key),grouping=>grouping.ToList())
.选择(perfsByProduction=>
新网络制作
{
ProductionSeasonId=perfsByProduction.Key.ProductionSeasonNumber,
持续时间=perfsByProduction.Key.Duration,
Title=perfsByProduction.Key.UserFriendlyTitle,
Synopsis=perfsByProduction.Key.UserFriendlySynopsis,
ThumbnailImage=perfsByProduction.Key.PreviewImage,
性能=perfsByProduction.Value
})
.ToList();

以下是lamba将第二个lambda表达式重构为“传统”代码:

IEnumerable groupedPerformance
=webPerformances.GroupBy(performance=>performance.ProductionSeasonId);
var dictionary=newdictionary();
foreach(GroupedPerformance中的I分组)
{
var group=新列表();
foreach(分组中的WebProduction WebProduction)
添加组(webProduction);
dictionary.Add(grouping.Key,group);
}
var result=新列表();
foreach(字典中的KeyValuePair项)
{
var wp=新的网络产品
{
ProductionSeasonId=perfsByProduction.Key.ProductionSeasonNumber,
持续时间=perfsByProduction.Key.Duration,
Title=perfsByProduction.Key.UserFriendlyTitle,
Synopsis=perfsByProduction.Key.UserFriendlySynopsis,
ThumbnailImage=perfsByProduction.Key.PreviewImage,
性能=perfsByProduction.Value
};
结果:添加(wp);
}

我不知道你的
SheddProduction.GetOurProduction方法的结果类型,因此我做了一些小改动,但你可以得到要点…

为什么你同时做
ToDictionary
GroupBy
ToDictionary
似乎没有必要。一本关于linq和惰性评估的好入门书:2800_和-in-in-contrast_2c00-eager-evaluation_2900_。aspxI创建了这个确切的示例,但你比我快了。@DVK比DVK的速度快!这对我有用。我能够将lambda表达式分解为单独的未约束表达式,然后我能够测量每个表达式的性能。这个问题与GetOurProduction方法有关,该方法多次调用web服务方法,然后调用数据库
IEnumerable<IGrouping<string, WebProduction>> groupedPerformances 
    = webPerformances.GroupBy(performance => performance.ProductionSeasonId);

var dictionary = new Dictionary<string, List<WebProduction>>();
foreach (IGrouping<string, WebProduction> grouping in groupedPerformances)
{
    var group = new List<WebProduction>();
    foreach (WebProduction webProduction in grouping)
        group.Add(webProduction);

    dictionary.Add(grouping.Key, group);
}

var result = new List<WebProduction>();
foreach (KeyValuePair<string, List<WebProduction>> item in dictionary)
{
    var wp = new WebProduction
    {
        ProductionSeasonId = perfsByProduction.Key.ProductionSeasonNumber,
        Duration = perfsByProduction.Key.Duration,
        Title = perfsByProduction.Key.UserFriendlyTitle,
        Synopsis = perfsByProduction.Key.UserFriendlySynopsis,
        ThumbnailImage = perfsByProduction.Key.PreviewImage,
        Performances = perfsByProduction.Value
    };
    result.Add(wp);
}