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
Performance LINQ中重用函数的结果_Performance_Linq_Entity Framework - Fatal编程技术网

Performance LINQ中重用函数的结果

Performance LINQ中重用函数的结果,performance,linq,entity-framework,Performance,Linq,Entity Framework,我以LINQ中的以下查询为例。是否可以保存GetCalendarResources函数的结果,这样我就不必多次调用它?谢谢 var query = from T in query2.AsEnumerable() select new Event { resource = GetCalendarResources(T.eventID), text = GetCalendarResources

我以LINQ中的以下查询为例。是否可以保存GetCalendarResources函数的结果,这样我就不必多次调用它?谢谢

var query = from T in query2.AsEnumerable()
            select new Event
            {
               resource = GetCalendarResources(T.eventID),
               text = GetCalendarResources(T.eventID) + T.eventName
            };
您可以使用关键字,这使您可以自由地在下一个级别使用其值:

var query = from T in query2.AsEnumerable()
            let res= GetCalendarResources(T.eventID)
            select new Event
            {
               resource =res,
               text = res + T.eventName
            };

谢谢工作得很好。我以前用过,但我忘了。这个查询效率很低。您正在进行n+1数据库调用,并且没有筛选。这不是我正在使用的实际查询。我只是写了一个例子。