Linq Keyword Exception - Limit Limit Area

Regarding this decision .

Is there a way to limit the number of keywords to consider? For example, I would like to calculate only the first 1000 words of the text. There is a “Take” method in Linq, but it serves a different purpose - all words will be computed and N records will be returned. What is the right alternative to do it right?

+3
source share
3 answers

Just apply Takebefore - right after the call Split:

var results = src.Split()
                 .Take(1000)
                 .GroupBy(...) // etc
+2
source

Enumerable.Take ; , N. , , , Take, String.Split. , - ; "", .

, :

var words = src.StreamingSplit()  // you'll have to implement that            
               .Take(1000);

, :

...
.GroupBy(str => str)   // group words by the value
.Select(g => new
             {
                str = g.Key,      // the value
                count = g.Count() // the count of that value
              });

, GroupBy - - , 1000 - .

, :

  • , src.Split().Take(1000) . ( , ) ( , ). , .
  • (1) - /, src.StreamingSplit().Take(1000) . , 1000 .

, 1000 GroupBy .

+1

Well, strictly speaking, LINQ will not necessarily read everything; Will be accepted as soon as possible. The problem is that in the related question you are looking at Count and it is impossible to get Count without consuming all the data. Similarly, string.Split will look at everything.

But if you wrote the lazy non-buffering Split function (using return returns) and you need the first 1000 unique words, then

var words = LazySplit(text).Distinct().Take(1000);

will work

+1
source

Source: https://habr.com/ru/post/1773490/


All Articles