如何合并两个IQueryable列表 [英] How to merge two IQueryable lists
问题描述
我想在C#中合并两个IQueryable列表的记录。我尝试
I want to merge the records of two IQueryable lists in C#. I try
IQueryable<MediaType> list1 = values;
IQueryable<MediaType> list2 = values1;
obj.Concat(obj1);
和
IQueryable<MediaType> list1 = values;
IQueryable<MediaType> list2 = values1;
obj.Union(obj1);
但是如果 list1
为空,则结果为列表也为空。就我而言, list1
可以为空,但 list2
可以有记录。我应该如何合并它们?
but if list1
is empty then the resultant list is also empty. In my case either list1
can be empty but list2
can have records. How should i merge them?
推荐答案
您不使用返回值-就像所有其他LINQ运算符一样,该方法不会不会更改现有序列-它返回一个 new 序列。因此,请尝试以下操作:
You're not using the return value - just like all other LINQ operators, the method doesn't change the existing sequence - it returns a new sequence. So try this:
var list3 = list1.Concat(list2);
或
var list4 = list1.Union(list2);
Union
是一个设置操作-返回
Union
is a set operation - it returns distinct values.
Concat
只是返回第一个序列中的项目,然后返回第二个序列中的项目;
Concat
simply returns the items from the first sequence followed by the items from the second sequence; the resulting sequence can include duplicate items.
您可以将 Union
视为 Concat
,后跟不同
。
这篇关于如何合并两个IQueryable列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!