Sometimes, I'm too clever for my own good...
-
It's official - I have too many eBooks to cope with without help, so I decided to knock up a quick SQL DB app to organise them (and play with creating a new DataGridView based UserControl with automatic fuzzy filtering). So I've just spent the last hour or so wondering where my data is / was / should be:
string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
IEnumerable<Book> books = files.Select(f => new Book(f, path));
Author.SaveAllNew();
Series.SaveAllNew();
Book.SaveAllNew();The Book constructor determines the Series and Author and creates them as needed. So where is everybody? My DB is still empty... ...later. ...much later. ...until I remembered Linq methods use deferred execution, that is... :doh:
string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
IEnumerable<Book> books = files.Select(f => new Book(f, path)).ToList();
Author.SaveAllNew();
Series.SaveAllNew();
Book.SaveAllNew();:sigh:
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
:confusedFaceAtMisleadingTitle:
-
It's official - I have too many eBooks to cope with without help, so I decided to knock up a quick SQL DB app to organise them (and play with creating a new DataGridView based UserControl with automatic fuzzy filtering). So I've just spent the last hour or so wondering where my data is / was / should be:
string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
IEnumerable<Book> books = files.Select(f => new Book(f, path));
Author.SaveAllNew();
Series.SaveAllNew();
Book.SaveAllNew();The Book constructor determines the Series and Author and creates them as needed. So where is everybody? My DB is still empty... ...later. ...much later. ...until I remembered Linq methods use deferred execution, that is... :doh:
string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
IEnumerable<Book> books = files.Select(f => new Book(f, path)).ToList();
Author.SaveAllNew();
Series.SaveAllNew();
Book.SaveAllNew();:sigh:
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
Assuming you're using .NET 4.0 or higher, Directory.EnumerateFiles[^] is more efficient than Directory.GetFiles[^], as it doesn't have to allocate an array to contain every file path. Also, unless you specifically want to filter out files without extensions, use
"*"
instead of"*.*"
as your search pattern. And since you don't seem to be using the result of theToList
call, it would probably be better to avoid creating a list. Unfortunately, there's no built-inConsume
method, but it's not hard to write one:public static class NomNomNom
{
public static void Consume(this IEnumerable source)
{
if (source == null) throw new ArgumentNullException("source");
foreach (T item in source);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Assuming you're using .NET 4.0 or higher, Directory.EnumerateFiles[^] is more efficient than Directory.GetFiles[^], as it doesn't have to allocate an array to contain every file path. Also, unless you specifically want to filter out files without extensions, use
"*"
instead of"*.*"
as your search pattern. And since you don't seem to be using the result of theToList
call, it would probably be better to avoid creating a list. Unfortunately, there's no built-inConsume
method, but it's not hard to write one:public static class NomNomNom
{
public static void Consume(this IEnumerable source)
{
if (source == null) throw new ArgumentNullException("source");
foreach (T item in source);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I use the list later on for UI updates - I just didn't show that part!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
-
Assuming you're using .NET 4.0 or higher, Directory.EnumerateFiles[^] is more efficient than Directory.GetFiles[^], as it doesn't have to allocate an array to contain every file path. Also, unless you specifically want to filter out files without extensions, use
"*"
instead of"*.*"
as your search pattern. And since you don't seem to be using the result of theToList
call, it would probably be better to avoid creating a list. Unfortunately, there's no built-inConsume
method, but it's not hard to write one:public static class NomNomNom
{
public static void Consume(this IEnumerable source)
{
if (source == null) throw new ArgumentNullException("source");
foreach (T item in source);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
One does not simply lecture OG on programming . :laugh:
Microsoft ... the only place where VARIANT_TRUE != true
Oh, come on! He describes himself as the CEO of Wales. When was the last time you met a CEO who knew the first thing about writing code? :rolleyes:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Oh, come on! He describes himself as the CEO of Wales. When was the last time you met a CEO who knew the first thing about writing code? :rolleyes:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
When was the last time you met a CEO who knew the first thing about writing code?
I couldn't know after all I work as a code monkey. I don't meet such people like CEOs. But your post was funny :D :thumbsup:
Microsoft ... the only place where VARIANT_TRUE != true
-
Richard Deeming wrote:
When was the last time you met a CEO who knew the first thing about writing code?
I couldn't know after all I work as a code monkey. I don't meet such people like CEOs. But your post was funny :D :thumbsup:
Microsoft ... the only place where VARIANT_TRUE != true
Argonia wrote:
I don't meet such people like CEOs
Undo another button or two.
You'll never get very far if all you do is follow instructions.
-
One does not simply lecture OG on programming . :laugh:
Microsoft ... the only place where VARIANT_TRUE != true
He can if he wants - I make mistakes like everyone else, and am both very aware and very thankful that I don't know everything. Mind you, I did know everything when I was 18... :laugh:
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
-
He can if he wants - I make mistakes like everyone else, and am both very aware and very thankful that I don't know everything. Mind you, I did know everything when I was 18... :laugh:
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
-
OriginalGriff wrote:
when I was 18...
That was surely before the dawn of men (and thus before the age of the personal computer) ;P
V.
(MQOTD rules and previous solutions)Pre-PC indeed, but not Pre-DoM: I'm not 'Enry Minute!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
-
Pre-PC indeed, but not Pre-DoM: I'm not 'Enry Minute!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)