Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No "MoveNext" to go with DeferredQuery? #340

Closed
pcolmer opened this issue Jan 18, 2015 · 4 comments
Closed

No "MoveNext" to go with DeferredQuery? #340

pcolmer opened this issue Jan 18, 2015 · 4 comments

Comments

@pcolmer
Copy link

pcolmer commented Jan 18, 2015

The comments for DeferredQuery say that:
/// An enumerable with one result for each row returned by the query.
/// The enumerator will call sqlite3_step on each call to MoveNext, so the database
/// connection must remain open for the lifetime of the enumerator.
However, there doesn't seem to be any method called MoveNext. The only references to sqlite3_step I can find are in the C mapping code.
Is this part of the library that hasn't been implemented yet?

@nurchi
Copy link

nurchi commented Feb 5, 2015

Not sure if this is what you want, but...

IEnumerator<T> _enumerator=db.Table<T>().AsEnumerable().GetEnumerator();
_enumerator.Reset();
while (_enumerator.MoveNext())
{
    // Do something with _enumerator.Current
    // OR:
    T myObj=_enumerator.Current;
    // now do something with myObj
}

(replace T with your desired type)

For those who don't know, .Reset() moves the "pointer" to the "before-the-first" element, so the first call to .MoveNext() starts at the actual first element in the collection.

@pcolmer
Copy link
Author

pcolmer commented Feb 7, 2015

It may just be that I am not understanding the code well enough. Deferred queries do seem to work - it is just that the comments make reference to MoveNext but I can't find anything in the actual source called MoveNext.

I'm more interested in using the deferred queries than ever after reading about how List is implemented and how there is a limit on the number of items a list can contain: http://www.codeproject.com/Articles/870013/List-T-Is-it-really-as-efficient-as-you-probably-t.

What I need to do is some timings and SQL tracing to find out more about how the deferred queries work and how the performance compares to the non-deferred ... which seem to call the deferred query mechanism and then create a List. I suspect that means that you've got the same performance/overhead involved in the deferred portion of the code, plus the overhead of creating the List.

@nurchi
Copy link

nurchi commented Feb 18, 2015

Well, if you don't like lists, you're free to use ToArray() method. I've read the article you linked and what can I say, the author admits that "... List is one of the most useful classes out there."

I disagree with him/her on the arrays though, in .NET array is a class, not just a simple sequence in the same sense it is in C/C++ (that's why you have methods and properties on the array like .Length and others).

List<T> is essentially a dynamic array, since "regular" arrays do not have the option of dynamically adding/removing elements from it. If you choose, you can also implement your own List class and as long as it implements IList<T>, you should be able to use LINQ functions on it too. Think you can do it more efficiently, accept the challenge! (just kidding) :)

When using arrays with unsafe code in C#, yes, some things are brought down to "the level of C" When you "fix" a pointer to an array, it tells GC not to collect or move that data, etc...

To be fair, I haven't looked at how .ToList() does its job, but I'm sure it knows in advance and pre-allocates the correct(-ish) number of elements for the list before populating it. I've used List<T> in a project with the SQLite database file sitting on a network share and it seemed to work without any issues (although, the DB file wasn't that big).

@praeclarum
Copy link
Owner

I updated the docs to try to make this clearer

github-actions bot pushed a commit to Reddevildragg-UPM-Forks/sqlite-net that referenced this issue Nov 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants