简体   繁体   English

无法从 LINQ 语句调用另一个方法

[英]Can't call another method from LINQ statement

I have ac# statement which iterate thru a collection of row.我有 ac# 语句,它遍历行的集合。 One of the fields call a private method to get an array of object but I am getting null.其中一个字段调用私有方法来获取对象数组,但我得到的是空值。 I placed a breakpoint inside the linq but it never hits the method.我在 linq 中放置了一个断点,但它从未命中该方法。

Here is my code这是我的代码

IQueryable<MyObject> myObject = ds.Tables['Table'].AsEnumerable().Select(row => new MyObject
{
  id = row.Field<int>("ID"),
  MyCollectionArray = this.getCollectionArray(row.Field<string>("MyAggregatedString")),
}).AsQueryable();

private MyObect[] getCollectionArray(string concatString)
{

  // placed a breakpoint, it is never called. Not sure why
}

Thanks for any asistance.感谢您的任何帮助。

What you are facing is called Deferred Execution .您面临的称为Deferred Execution
Which means that your query is not executed until you use it somewhere.这意味着您的查询在您在某处使用它之前不会执行。
Here is a part from the documentation:这是文档的一部分:

Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required.延迟执行意味着表达式的计算被延迟到实际需要它的实现值。 Deferred execution can greatly improve performance when you have to manipulate large data collections, especially in programs that contain a series of chained queries or manipulations.当您必须操作大型数据集合时,延迟执行可以极大地提高性能,尤其是在包含一系列链式查询或操作的程序中。 In the best case, deferred execution enables only a single iteration through the source collection.在最好的情况下,延迟执行只允许通过源集合进行一次迭代。

And in order to execute your statement you just have to use it.为了执行你的语句,你只需要使用它。 The simplest way might be just by calling最简单的方法可能只是通过调用

myObject.ToList()

Also it will be executed in case you use functions which require the values to be populated (like Sum, Average, etc)如果您使用需要填充值的函数(如 Sum、Average 等),它也会被执行

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

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