4

I have 2 instances of a class that implements the IEnumerable interface. I would like to create a new object and combine both of them into one. I understand I can use the for..each to do this.

Is there a linq/lambda expression way of doing this?

EDIT

public class Messages : IEnumerable, IEnumerable<Message>
{
  private List<Message> message = new List<Message>();

  //Other methods
}

Code to combine

MessagesCombined messagesCombined = new MessagesCombined();

MessagesFirst messagesFirst = GetMessageFirst();
MessagesSecond messagesSecond = GetMessageSecond();

messagesCombined = (Messages)messagesFirst.Concat(messagesSecond); //Throws runtime exception

//Exception is

Unable to cast object of type '<ConcatIterator>d__71`1[Blah.Message]' to type 'Blah.Messages'.
2
  • IEnumerable or IEnumerable<T>? And do you mean a set join, union, or intersect? Commented Apr 8, 2010 at 15:04
  • I looked again and surprisingly, it implements BOTH IEnumerable and IEnumerable<T>. Didn't know we could do that. I mean set union. Commented Apr 8, 2010 at 15:09

4 Answers 4

6

I had the same problem with an array of byte. What I did to solve my issue:

col1.Concat(col2).ToArray();

If you got a list:

col1.Concat(col2).ToList();
Sign up to request clarification or add additional context in comments.

Comments

2

Try something like this:

var combined = firstSequence.Concat(secondSequence);

This is using the Enumerable.Concat extension method.

3 Comments

Why are you creating custom classes for set of messages? Just use IEnumerable<Message> for all 3. The problem you are having is that you are trying to assign Messages to an instance of something more derived, which is not allowed.
>> Why are you creating custom classes for set of messages? I am working with existing types. I am not the author of Messages and Message class.
@DotnetDude, so how did you beat the problem? Why did you chose the answer — aren't that didn't worked?
1

The Enumarable.Concat method returns an IEnumerable<Message> (or in fact an <ConcatIterator>d__71<Message> as the exception message shows). You can not cast that to your Messages type. You can do the following:

var m = new Messages(messagesFirst.Concat(messagesSecond));

And make sure your Messages type has a constructor taking an IEnumerable<Message>:

public class Messages : IEnumerable, IEnumerable<Message>
{
    private List<Message> message;

    public Message(IEnumerable<Message> messages)
    {
        this.message = new List<Message>(messages);
    }

    //Other methods
}

8 Comments

@Steven - I need the result to be of type Messages. var wouldn't work for me.
Well, it is of type Messages. "var" just instructs the compiler to "guess" the most correct type according to what's on the right side of the statement; in this case, the creation of a Messages object. It's called "type inference", and it's really useful.
@Smith - So, if the combining logic is in a method with return param of Messages, can I return a object of type "var" without the compiler complaining or any exceptions at runtime? Also, I do further processing of the combined list, so at some point i have to cast it to Messages.
From MSDN: "Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type." If I write"var i = 2;", then i will be of type int, because 2 is of type int. See here: msdn.microsoft.com/en-us/library/bb383973.aspx
@Smith - Thanks for the explanation. I do understand that the type of var is the type that is inferred from the right side of the equation. However, the issue i have is that I cannot return an object of type var from the method with return type of Members.
|
1

You will want to use Concat.

Pet[] cats = GetCats();
Pet[] dogs = GetDogs();

IEnumerable<Pet> query = cats.Concat(dogs);

Per your edit:

IEnumerable<Message> messagesCombined;

MessagesFirst messagesFirst = GetMessageFirst();
MessagesSecond messagesSecond = GetMessageSecond();

// if this doesn't work, you can cast both MessagesFirst and MessagesSecond to IEnumerable<Message>
messagesCombined = messagesFirst.Concat(messagesSecond);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.