Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ选择不同于4个IEnumerable列表_C#_Linq_Ienumerable_Distinct - Fatal编程技术网

C# LINQ选择不同于4个IEnumerable列表

C# LINQ选择不同于4个IEnumerable列表,c#,linq,ienumerable,distinct,C#,Linq,Ienumerable,Distinct,假设有四个列表,它们至少都具有此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&g

假设有四个列表,它们至少都具有此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>();
公共A类//或B、C、D类
{
公共字符串Id{get;set;}
//…其他财产
}
//因此:
列表a1=新列表();
列表a2=新列表();
列表a3=新列表();
列表a4=新列表();
我想在以下位置选择所有不同的ID: a1,与a2、a3和a4组合


我认为LINQ语法是理想的,但是如何将它们组合成具有单个字符串属性的IEnumerable结果,例如具有类a定义的内容。

您可以包含相同类型的IEnumerable,然后选择所需的项

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

编辑:根据新问题。。。无法在单个查询中从不同的列表类型中进行选择。最好的方法是使用包含所选属性的公共类型,然后使用基本类型运行上述查询。

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

List<A> a1 = new List<A>();
List<B> a2 = new List<B>();
List<C> a3 = new List<C>();
List<D> a4 = new List<D>();
这也适用于:

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

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

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();
联合 当然,您可以简化此操作:

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();
通过使用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);

由于a1、a2、a3和a4是不同的类型,因此隐式类型数组不会有“最佳类型”匹配。。。编译器错误CS0826()在我的回答之后澄清了a1、a2、a3和a4可能有不同的类型。与我最终得出的结果完全相同。这个问题的第一个版本并没有完全清楚地表明它们是不同的类型。
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);