C# 如何初始化为空的 IEnumerable<Object> 并允许连接到它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17831011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:36:10  来源:igfitidea点击:

How to initialize IEnumerable<Object> that be empty and allow to Concat to it?

c#linqlinq-to-entitiesconcatenationargumentnullexception

提问by Majid

I tried this code for adding bto books:

我尝试使用此代码添加bbooks

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

System.ArgumentNullException:值不能为空。参数名称:第一个

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

似乎nullCollection无法串联。我使用 EF,那么我应该如何初始化我的Collection那些没有东西的东西,我可以连接到它吗?

采纳答案by Rodrigo López

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

它接缝您想要做的就是按某些标准过滤您的 context.Books。

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

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

如果您仍然需要空的 IEnumerable,您可以调用 Enumerable.Empty():

IEnumerable<Book> books = Enumerable.Empty<Book>();

回答by chris.ellis

IEnumerable<Book> books = new List<Book>();

回答by Jakub Dropia

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

您需要将书籍创建为一个 IEnumerable 空对象,如 List,但需要记住在循环后调用 ToList() 对书籍。例如:

        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();
        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();

回答by Matthew Watson

Personally I'd just go with:

就我个人而言,我只会选择:

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

rather than using a List.

而不是使用列表。

回答by AnorZaken

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:

或者,如果您想从 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.

...虽然我有几个问题,为什么您想要/需要以这种方式做事。