【问题标题】:How do I merge two seperate '.ToList()"?如何合并两个单独的 '.ToList()"?
【发布时间】:2020-09-19 15:15:59
【问题描述】:

有几个帖子有人在问how to merge generic lists。 但是,在我的情况下,我有两个单独的 linq 语句,我在其中返回两个单独的数据库结果 .ToList()

var appleList = (from al in db.Apples where al.Id == id select al).ToList();
var bananaList = (from bl in db.Bananas where bl.Id == id select bl).ToList();

我尝试像这样合并这些列表:

appleList.Concat(bananaList);

但这会导致以下错误:

“列表”不包含“Concat”的定义

如何将这两个.ToList()结合起来?

【问题讨论】:

  • appleList.AddRange(bananaList);,试试这个
  • 当我这样做时,它说cannot convert from System.Collections.Generic.List<ProjectName.Models.Bananas> to System.Collections.Generic.IEnumerable<Project.Model>
  • @SowmyadharGourishetty AppleBanana 可能是不同的类型。不能将两种不同类型的实例添加到同一个 List 中,除非 List 是一些常见的超类型。
  • @Skullomania,我的错,我没有看到它们是 2 种不同的类型。你不能合并你的情况,因为它们是不同的类型

标签: asp.net-mvc list linq


【解决方案1】:

创建一个水果类并从中派生苹果和香蕉。

https://www.webopedia.com/TERM/B/base_class.html

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    相关资源
    最近更新 更多