50

I tried this code for adding b to books:

IEnumerable<Book> books =null;
foreach (Book b in context.Books.AsEnumerable())
    if (someConditions)
       books = books.Concat(new[] {b});

but gives me this error on last line of code:

System.ArgumentNullException: Value cannot be null. Parameter name: first

it seems that null Collection could not concatenated. I use EF,so how should I initialize my Collection that have no thing in it and I could concatenate to it?

10
  • you can not run method Concat on null Commented Jul 24, 2013 at 10:13
  • 1
    IEnumerable<Book> books = new List<Book>();
    – Nick
    Commented Jul 24, 2013 at 10:14
  • 30
    IEnumerable<Book> books = Enumerable.Empty<Book>(); Commented Jul 24, 2013 at 10:15
  • 3
    @VyacheslavVolkov That should be an answer. Commented Jul 24, 2013 at 10:18
  • 3
    Others have answered your question. But it's still very inefficient, and somewhat ugly, to create an IEnumerable<Book> by concatting a lot of length-1 arrays of type Book[]. Consider filtering your books with Linq Where as in @Damith's comment, or maybe rewrite to use yield return b; from the loop (extract this loop to a new method, an iterator block). Commented Jul 24, 2013 at 10:49

5 Answers 5

114

It seams all you want to do is filter your context.Books by some criteria.

IEnumerable<Book> books = context.Books.Where(b => someConditions);

If you still need the empty IEnumerable you can just call Enumerable.Empty():

IEnumerable<Book> books = Enumerable.Empty<Book>();
0
28
IEnumerable<Book> books = new List<Book>();
0
5

This is what you are trying to do:

IEnumerable<Book> books = Enumerable.Empty<Book>();
books = books.Concat(context.Books.AsEnumerable().Where(b => someCondition));

Alternatively you can do this if you like to start from null:

IEnumerable<Book> books = null;
var moreBooks = context.Books.AsEnumerable().Where(b => someCondition);
books = books == null ? moreBooks : books.Concat(moreBooks);

...although I have several question as to why you want / need to do things this way.

4

Personally I'd just go with:

IEnumerable<Book> books = new Book[0];

rather than using a List.

3
  • 1
    Although I would still use Enumerable.Empty<T> rather than this, because it avoids creating new array instances, this is still better than creating a List<T> as per the accepted answer because there is an optimization in .net that caches Enumerators for zero length arrays!
    – AnorZaken
    Commented Apr 4, 2016 at 1:34
  • @AnorZaken I do agree that Enumberable.Empty<T> would be better (since it's more descriptive), but note that it does still (lazily) create an empty array. Commented Apr 4, 2016 at 8:21
  • 1
    Yes but it does so only once for each type T, using the type-system and JIT to make it lazy and cached (generic static readonly field). So Enumerable.Empty still comes out on top. (We don't know how many times that line there will be called - maybe a 1000 times per second, only OP knows.)
    – AnorZaken
    Commented Apr 5, 2016 at 14:51
1

You need create books as a IEnumerable empty object like List, but need remember to call, after loop, ToList() on books. For example:

        IEnumerable<int> books = new List<int>();
        IEnumerable<int> books2 = new int[] { 1, 2, 3, 4 };


        foreach (int b in books2)
            if (b > 2)
                books = (new[] { b }).Concat(books);

        books = books.ToList();
1
  • 3
    Or use Enumerable.Empty<Book>() like Vyacheslav Volkov mentioned above.
    – darthkurak
    Commented Jul 24, 2013 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.