简体   繁体   English

存储 LINQ 的最佳类型导致全局变量

[英]Best type to store a LINQ result in a global variable

I'm writing a report and loading the data to display as soon as the report loads using LINQ.我正在编写报告并加载数据以在报告使用 LINQ 加载后立即显示。

summaryData = from summary in _entity.Summary
              // some constraints here
              select new
              {
                  summary.payment_category_desc,
                  summary.payment_due_amt,
                  summary.currency_desc
              };            

I need to consume these results later in a different event(the PrintDetail), in a way that I can, for instance, say:我需要稍后在不同的事件(PrintDetail)中使用这些结果,例如,我可以说:

string name = summaryData[currentIndex].payment_category_desc

Basically what I need is to find how to store the LINQ results in a global variable that I can later enumerate.基本上我需要的是找到如何将 LINQ 结果存储在一个全局变量中,我以后可以枚举该变量。 I've tried IQueriable and it wont let me.我已经尝试过 IQueriable,但它不会让我这样做。 I'm also trying to avoid having to create a class with three members in order to be able to use ToList().我还试图避免创建一个包含三个成员的 class 以便能够使用 ToList()。

Any thoughts?有什么想法吗?

Edit To be honest, I only just noticed that you were using an anonymous types.编辑说实话,我只是注意到您使用的是匿名类型。 As you already know by now, there is no way you can get an anon-type outside your method.正如您现在已经知道的那样,您无法在您的方法之外获得匿名类型。 However, you can use a C# 4.0 Tuple<>但是,您可以使用 C# 4.0 元组<>

The following will work when summaryData is passed/defined outside you current method:当 summaryData 在当前方法之外传递/定义时,以下内容将起作用:

summaryData = from summary in _entity.Summary
          // some constraints here
          select new Tuple<string, DateTime, decimal>
          {
              summary.payment_category_desc,
              summary.payment_due_amt,
              summary.currency_desc
          };            

Whatever it is, if it is a collection/query result, IEnumerable<T> will work不管它是什么,如果它是一个集合/查询结果, IEnumerable<T>将起作用

Be sure to think whether you need to convert to a list (cache the result) with .ToList() first.请务必先考虑是否需要先使用.ToList()转换为列表(缓存结果)。 If you don't the original enumerator will get executed multiple times, which could be a (performance) issue.如果您不这样做,原始枚举器将被执行多次,这可能是一个(性能)问题。 Consider when the enumerator accesses a file that is not longer opened, accessible etc.考虑当枚举器访问不再打开、无法访问等的文件时。

Personally I'd avoid using a global variable.我个人会避免使用全局变量。 You make it harder to test your application when you have a dependency such as a global variable.当您有诸如全局变量之类的依赖项时,您会更难测试您的应用程序。

I don't know your exact architecture but I'd be tempted to pass the object to the relevant methods that require it.我不知道您的确切架构,但我很想将 object 传递给需要它的相关方法。 At least this way you can easily mock your object and you reduce the dependency.至少通过这种方式,您可以轻松地模拟您的 object 并减少依赖性。

Your IQueryable result employs deffered execution.您的 IQueryable 结果采用延迟执行。 If you are coding that inside of a "using" block, you won't have a connection when you try to access your results (global variable or not).如果您在“使用”块内对其进行编码,则当您尝试访问结果(是否为全局变量)时,您将无法建立连接。

Try returning a List from your LINQ query via the ToList() extension method.尝试通过 ToList() 扩展方法从 LINQ 查询返回列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM