使用 linq 组合对象

发布于 2024-08-28 07:32:40 字数 758 浏览 4 评论 0原文

我有两个实现 IEnumerable 接口的类实例。我想创建一个新对象并将它们合并为一个。我知道我可以使用 for..each 来执行此操作。

有 linq/lambda 表达式的方法可以做到这一点吗?

编辑

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

  //Other methods
}

代码以组合

MessagesCombined messagesCombined = new MessagesCombined();

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

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

//异常是

Unable to cast object of type '<ConcatIterator>d__71`1[Blah.Message]' to type 'Blah.Messages'.

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'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彼岸花似海 2024-09-04 07:32:40

我对字节数组也有同样的问题。我做了什么来解决我的问题:

col1.Concat(col2).ToArray();

如果你有一个列表:

col1.Concat(col2).ToList();

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();
故人爱我别走 2024-09-04 07:32:40

尝试这样的操作:

var combined = firstSequence.Concat(secondSequence);

这是使用 Enumerable.Concat 扩展方法。

Try something like this:

var combined = firstSequence.Concat(secondSequence);

This is using the Enumerable.Concat extension method.

孤蝉 2024-09-04 07:32:40

Enumarable.Concat 方法返回一个 IEnumerable (或者实际上是一个 d__71,如异常消息所示)。您不能将其转换为 Messages 类型。您可以执行以下操作:

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

并确保您的 Messages 类型具有采用 IEnumerable 的构造函数:

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

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

    //Other methods
}

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
}
梦明 2024-09-04 07:32:40

您将需要使用Concat

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

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

根据您的编辑:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文