3

I have an object result of type IEnumerable and an object res of type Object. I want to concatenate both, how can i do

IEnumerable<V_Student_Attendace_DayWise> result;
V_Student_Attendace_DayWise res;
result.Concat(res); // Error here...
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Usf Noor
  • 219
  • 1
  • 5
  • 21
  • Possible duplicate of [What's the Best Way to Add One Item to an IEnumerable?](http://stackoverflow.com/questions/15251159/whats-the-best-way-to-add-one-item-to-an-ienumerablet) – Duc Filan Dec 08 '16 at 05:43
  • 1
    What kind of error do you get? If you try exactly like in your code snippet, then you try to work with uninitialized variables. This is not possible in C#. – abto Dec 08 '16 at 05:45

3 Answers3

2

The Enumerable.Concat method expect a collection as its argument, so you have to give the input as a collection in order to get it concatinated. So the code for this will looks like the following, where res the object that you already having

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

You can try this as well:

result = result.Concat(new[] { new V_Student_Attendace_DayWise()});
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
1

Try this,

result.ToList().Add(res);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

The answers given are good, but please be aware that you're not tacking new items onto the end of your IEnumerable. You are creating a new collection that consists of the old collection with the new item tacked on at the end. (If the collection that implements IEnumerable happens to implement IList as well, you could cast and Add for improved performance and memory usage, but different semantics.)

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42