【发布时间】: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
Apple和Banana可能是不同的类型。不能将两种不同类型的实例添加到同一个 List 中,除非 List 是一些常见的超类型。 -
@Skullomania,我的错,我没有看到它们是 2 种不同的类型。你不能合并你的情况,因为它们是不同的类型
标签: asp.net-mvc list linq