简体   繁体   English

通过linq从多个列表中选择项目

[英]Select items from multiple lists via linq

I have the following issue: 我有以下问题:

class Base
{
   int val;
}
class A : Base
{
}
class B : Base
{
}

//Now I want to select from list of a and b
List<A> aList;
List<B> bList;
IEnumerable<Base> Find(int i)
{
//would need something like this
  return from a in (aList and bList) where a.val == i select a as Base;
}

What would be the fastest solution for this ? 最快的解决方案是什么? Should I join the enumerations afterwards or is that possible within the linq query ? 我应该在之后加入枚举,还是在linq查询中可行?

edit: Would .Concat be the quickest way ? 编辑:.Concat是最快的方法吗?

How about: 怎么样:

return lista.Cast<Base>().Concat(listb).Where( x => x.val == i);

Cast<Base> is necessary to have a list of homogenous types, and Concat is the same as Union but will not incur the overhead of duplicate elimination. 必须具有Cast<Base>才能具有同类类型的列表,并且Concat与Union相同,但不会产生重复消除的开销。

First off, use a .OfType method to cast everything and allow each collection to be the same type. 首先,使用.OfType方法强制转换所有内容,并使每个集合都具有相同的类型。 This will only perform an explicit cast on objects already of the desired type, and return only objects of the desired type - I find it perfect for upcasting situations like this. 这只会对已经具有所需类型的对象执行显式转换,并且仅返回所需类型的对象-我发现它非常适合这种情况。

The second collection does not need to be cast, however - because of the definition of IEnumerable<out T> uses out you can allow polymorphism to act on the second collection properly. 第二个集合不需要强制转换,但是-由于IEnumerable<out T>使用out的定义,您可以允许多态正确地作用于第二个集合。 See Covariance and Contravariance for more info on this behaviour. 有关此行为的更多信息,请参见协方差和逆方差

If you want everything returned regardless of comparison, use .Concat 如果要返回所有结果而不考虑比较,请使用.Concat

aList.OfType<Base>()
.Contat(bList)

If you want each item returned only once, use .Union 如果您希望每个项目只返回一次,请使用.Union

aList.OfType<Base>()
.Union(bList)

I Personally recommend Concat over Union , since it simply returns every element, whereas Union does the overhead of checking for duplicates, a task better suited to the .Distnict Method 我个人推荐Concat不是Union ,因为它只返回每个元素,而Union会执行检查重复项的开销,这项任务更适合于.Distnict方法

You can use Union 您可以使用联盟

return from a in (aList.Union<Base>(bList)) where a.val == i select a as Base;

Produces the set union of two sequences by using the default equality comparer. 通过使用默认的相等比较器产生两个序列的集合并集。

http://msdn.microsoft.com/en-us/library/vstudio/bb341731(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/vstudio/bb341731(v=vs.100).aspx

UPDATE UPDATE

Union will return all of the unique elements in both lists (that is, if the same element were in both lists, only one of them would be returned Union将返回两个列表中的所有唯一元素(也就是说,如果两个列表中都包含相同的元素,则仅返回其中一个)

The Concat(IEnumerable, IEnumerable) method differs from the Union method because the Concat(IEnumerable, IEnumerable) method returns all the original elements in the input sequences. Concat(IEnumerable,IEnumerable)方法不同于Union方法,因为Concat(IEnumerable,IEnumerable)方法返回输入序列中的所有原始元素。 The Union method returns only unique elements. Union方法仅返回唯一元素。

http://msdn.microsoft.com/en-us/library/vstudio/bb302894(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/vstudio/bb302894(v=vs.100).aspx

If this behavior is not desired, use Concat 如果不需要此行为,请使用Concat

return from a in (aList.Concat<Base>(bList)) where a.val == i select a as Base;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM