Questions tagged [take]

Take is a common keyword or function name used to return a given number of elements, usually contiguously and from the beginning of a collection. Only use this tag for the take keyword or function name; usage such as "take a picture/screenshot" is out of scope.

Take functions usually only require two pieces of information: the number of elements to be returned from the sequence, and the sequence itself.

If take is a method in a particular library or language, it is called on the object and would only require the number parameter.

This concept is found in many languages and libraries, some of which are listed below.

Reference

142 questions
46
votes
7 answers

LINQ Partition List into Lists of 8 members

How would one take a List (using LINQ) and break it into a List of Lists partitioning the original list on every 8th entry? I imagine something like this would involve Skip and/or Take, but I'm still pretty new to LINQ. Edit: Using C# / .Net…
Pretzel
  • 8,141
  • 16
  • 59
  • 84
43
votes
4 answers

RxJava2 observable take throws UndeliverableException

As I understand RxJava2 values.take(1) creates another Observable that contains only one element from the original Observable. Which MUST NOT throw an exception as it is filtered out by the effect of take(1) as it's happened second. as in the…
abd3lraouf
  • 1,438
  • 1
  • 18
  • 24
35
votes
4 answers

LINQ Take(); how to handle when null or fewer records than requested available?

I want to filter my results to take only the X amount of records. I am wondering how does Take() work? On this site I found: http://www.hookedonlinq.com/TakeOperator.ashx It says Take() "Throws an ArgumentNullException if source is null." So what…
chobo2
  • 83,322
  • 195
  • 530
  • 832
27
votes
6 answers

how to `.Take()` on a string and get a string at the end?

LINQ to Objects supports queries on string objects but when I use code such as below: string SomeText = "this is some text in a string"; return SomeText.Take(6).ToString(); All I get…
rtpHarry
  • 13,019
  • 4
  • 43
  • 64
26
votes
2 answers

How can I write Take(1) in query syntax

Is it possible to write IQueryable = query.Take(1) or something equivalent in LINQ query syntax. I'm using C# 5 and EF 5.
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80
26
votes
1 answer

LINQ with Skip and Take

I used the below code to take some items from IEnumerable, but it is always returning the source as null and count as 0 and actually there are items exists in IEnumerable private void GetItemsPrice(IEnumerable items, int customerNumber) { …
user1618825
21
votes
7 answers

C# Enumerable.Take with default value

What is the best way to get exactly x values from an Enumerable in C#. If i use Enumerable .Take() like this: var myList = Enumerable.Range(0,10); var result = myList.Take(20); The result will only have 10 elements. I want to fill the missing…
HectorLector
  • 1,851
  • 1
  • 23
  • 33
18
votes
2 answers

Spark: Difference between collect(), take() and show() outputs after conversion toDF

I am using Spark 1.5. I have a column of 30 ids which I am loading as integers from a database: val numsRDD = sqlContext .table(constants.SOURCE_DB + "." + IDS) .select("id") .distinct .map(row=>row.getInt(0)) This is the…
Christos Hadjinikolis
  • 2,099
  • 3
  • 20
  • 46
11
votes
3 answers

How much is performance improved when using LIMIT in a SQL sentence?

Let's suppose I have a table in my database with 1.000.000 records. If I execute: SELECT * FROM [Table] LIMIT 1000 Will this query take the same time as if I have that table with 1000 records and just do: SELECT * FROM [Table] ? I'm not looking…
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
7
votes
0 answers

spark warning "block locks were not released by tid" leads to poor performance

I'm learning spark. I'm trying to demonstrate a simple clustering algorithm. Let's say I find k items on my rdd that represent a cluster for me. The problem is when I try to take() them to the driver, I get a warning saying " block locks were not…
Achilles
  • 69
  • 3
7
votes
3 answers

Implementing take using foldr

This is my take version using foldr: myTake n list = foldr step [] list where step x y | (length y) < n = x : y | otherwise = y main = do print $ myTake 2 [1,2,3,4] The output is not what I…
badmaash
  • 4,775
  • 7
  • 46
  • 61
6
votes
3 answers

How to get total results count before .Take() - but when using .Take()

I am using .Take() to get a fixed number of results. What is the best way to get the TotalCountBeforeTake (ie as if I didn't use the .Take())? Can I get the TotalCountBeforeTake without running the query twice? var Results = (from results…
Ian G
  • 29,468
  • 21
  • 78
  • 92
5
votes
3 answers

I have some problems with LINQ expression, OrderBy(), Skip(), Take() works incorrect

I have LINQ expression like var a = ctx.EntitySet .OrderByDescending(t => t.Property) .Skip(pageIndex * size) .Take(size); OrderBy() should call before Skip() and Take(), but sorting happens at the end. Can I solve this…
DraggonZ
  • 1,057
  • 1
  • 16
  • 22
5
votes
1 answer

Array.Copy vs Skip and Take in c#

I was browsing this question and some similar ones: Getting a sub-array from an existing array Many places I read answers like this: Getting a sub-array from an existing array What I am wondering is why Skip and Take are not constant time operations…
Rob Hinchliff
  • 452
  • 1
  • 8
  • 14
5
votes
1 answer

Take is not a function : Angular 7 | Observable.take throws runtime error

I am building a shopping cart and I have used a shopping cart service in which I am assigning the quantities to a product/adding a product to the cart. Is there any other way than to use take for getting the first instance of the observable item$ ?…
1
2 3
9 10