如何向 IEnumerable 添加一行

How to add a row to IEnumerable

我有一个 IEnumerable 类型的对象 result 和一个 Object 类型的对象 res。 我想连接两者,我该怎么做

IEnumerable<V_Student_Attendace_DayWise> result;
V_Student_Attendace_DayWise res;
result.Concat(res); // Error here...

试试这个,

result.ToList().Add(res);

Enumerable.Concat 方法需要一个集合作为其参数,因此您必须将输入作为一个集合来进行连接。因此,此代码如下所示,其中 res 您已经拥有的对象

result = result.Concat(new[] { res});

你也可以试试这个:

result = result.Concat(new[] { new V_Student_Attendace_DayWise()});

给出的答案很好,但请注意,您并不是在 IEnumerable 的末尾添加新项目。您正在创建一个 new 集合,其中包含旧集合,最后添加了新项目。 (如果实现 IEnumerable 的集合碰巧也实现了 IList,您可以转换和 Add 以提高性能和内存使用,但语义不同。)