C# LINQ 从 4 个 IEnumerable 列表中选择不同的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/968281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:27:45  来源:igfitidea点击:

LINQ selecting distinct from 4 IEnumerable lists

c#linqienumerabledistinct

提问by CRice

Imagine four lists, all at least have this Id string property, but may have other properties:

想象四个列表,都至少有这个 Id 字符串属性,但可能有其他属性:

public class A //or B, C, D
{
    public string Id { get; set; }
    //..other properties
}

//so:  
List<A> a1 = new List<A>();
List<B> a2 = new List<B>();
List<C> a3 = new List<C>();
List<D> a4 = new List<D>();

I want to select all DISTINCT ids in: a1, combined with a2, a3 and a4

我想选择以下所有 DISTINCT id:a1,结合 a2、a3 和 a4

I thought LINQ syntax would be ideal but how do you combine these to an IEnumerable result with a single string property, for example something with a definition of class A.

我认为 LINQ 语法是理想的,但是如何将这些与单个字符串属性结合到 IEnumerable 结果中,例如具有 A 类定义的内容。

采纳答案by CoderDennis

Are they really all lists of the same type? Or do you have

它们真的都是同一类型的列表吗?或者你有

List<A> a1 = new List<A>();
List<B> a2 = new List<B>();
List<C> a3 = new List<C>();
List<D> a4 = new List<D>();

and you want to combine those because all 4 types have a common string Id property?

并且您想将它们结合起来,因为所有 4 种类型都有一个共同的字符串 Id 属性?

OK, your edit shows I was on the right track. You need an Interface that defines the string Id property and each type A, B, C and D need to implement that Interface.

好的,您的编辑表明我走在正确的轨道上。您需要一个定义字符串 Id 属性的接口,并且每个类型 A、B、C 和 D 都需要实现该接口。

Then each list will be of the Interface type and the concat answers will work.

然后每个列表都将是 Interface 类型,并且 concat 答案将起作用。

If you don't have ability to define the interface, then you could use LINQ to query each list for only the Id field (you should get IQueryable<String>results) and then concat those results.

如果您没有定义接口的能力,那么您可以使用 LINQ 来查询每个列表的 Id 字段(您应该得到IQueryable<String>结果),然后连接这些结果。

Edit: Here's a LINQ query that should give you what you're looking for:

编辑:这是一个 LINQ 查询,应该可以为您提供所需的信息:

var uniqueIds = (from a in a1
                 select a.Id).Concat(
                 from b in a2
                 select b.Id).Concat(
                 from c in a3
                 select c.Id).Concat(
                 from d in a4
                 select d.Id).Distinct();

回答by Cameron MacFarland

You can concat IEnumerables of the same type, and then select the item you need.

您可以连接相同类型的 IEnumerables,然后选择您需要的项目。

var query = a1
    .Concat(a2)
    .Concat(a3)
    .Concat(a4)
    .Select(a => a.ID)
    .Distinct();

EDIT: Based on the new question... Selecting from different list types isn't possible in a single query. The best way to do that would be to have a common type that includes the property you're selecting and then run the above query with the base type.

编辑:基于新问题......在单个查询中无法从不同的列表类型中进行选择。最好的方法是拥有一个包含您选择的属性的通用类型,然后使用基本类型运行上述查询。

回答by idursun

This also works:

这也有效:

new[] {a1, a2, a3, a4}.SelectMany(x => x.Select(y => y.Id)).Distinct();

回答by CMS

Since they are different types, I would select first the Id propertyto get an IEnumerable<string>, and then concatenate the results:

由于它们是不同的类型,我将首先选择Id 属性来获取 IEnumerable<string>,然后连接结果:

var query = a1.Select(a=> a.Id)
              .Concat(a2.Select(b=> b.Id))
              .Concat(a3.Select(c=> c.Id))
              .Concat(a4.Select(d=> d.Id))
              .Distinct();

回答by Don Gillis

Union

联盟

Of course, you can simplify this:

当然,你可以简化这个:

var uniqueIds = (from a in a1
                 select a.Id).Concat(
                 from b in a2
                 select b.Id).Concat(
                 from c in a3
                 select c.Id).Concat(
                 from d in a4
                 select d.Id).Distinct();

By using Union (which does an implicit 'Distinct'), to become:

通过使用 Union(它做了一个隐式的“Distinct”),变成:

var uniqueIds =  (from a in a1
                 select a.Id).Union(
                 from b in a2
                 select b.Id).Union(
                 from c in a3
                 select c.Id).Union(
                 from d in a4
                 select d.Id);