C# - 找出两个名单,其中普通成员; T> S - lambda语法语法、成员、名单、两个

2023-09-04 02:47:01 作者:距离式等待

所以我写了这个简单的控制台应用程序在我的问题,要求提供帮助。什么是使用的方法的第3行的lambda EX pression得到普遍成员的正确方法。尝试了加入(),但无法找出正确的语法。作为跟进...有一种非LINQ的方式来做到这一条线,我错过了?

 类节目
{
    静态无效的主要(字串[] args)
    {
        名单< INT> C =新的名单,其中,INT>(){1,2,3};
        名单< INT> A =新的名单,其中,INT>(){5,3,2,4};
        IEnumerable的< INT> J = c.Union&其中; INT>(一);
        //你只要给我伯爵
        Console.Write(j.ToList&其中; int的>()Count.ToString());

    }
}
 

解决方案

您想要相交()

 的IEnumerable< INT> J = c.Intersect(一);
 
SQL如何从一个ACCESS 数据库的表Table1里找出Field1,Field2,Field3相同的记录中Field4最大的那条记录

下面是基于在评论中提到的思想的 OrderedIntersect()的例子。如果你知道你的序列是有序的,应该跑得更快— O(N),而不是什么 .Intersect()通常是(不记得了我的头顶部)。但是,如果你不知道他们是有序的,它可能不会返回正确的结果在所有的:

 公共静态的IEnumerable< T> OrderedIntersect< T>(这IEnumerable的< T>源,IEnumerable的< T>其他),其中T:IComparable的
{
    使用(VAR XE = source.GetEnumerator())
    使用(VAR你们= other.GetEnumerator())
    {
        而(xe.MoveNext())
        {
           而(ye.MoveNext()&安培;&安培; ye.Current.CompareTo(xe.Current)℃,)
           {
              //什么也不做 - 我们所关心这里要说的是,我们提前在y枚举
           }
           如果(ye.Current.Equals(xe.Current))
              收益回报xe.Current;
           其他
           {// y为现在和GT; X,因此得到对X陷入再次
              而(xe.MoveNext()&安培;&安培; xe.Current.CompareTo(ye.Current)℃,)
              {} //又:只是前进,不要做任何事情

              如果(xe.Current.Equals(ye.Current))收益回报xe.Current;
           }

        }
    }
}
 

So I wrote this simple console app to aid in my question asking. What is the proper way to use a lambda expression on line 3 of the method to get the common members. Tried a Join() but couldn't figure out the correct syntax. As follow up... is there a non-LINQ way to do this in one line that I missed?

class Program
{
    static void Main(string[] args)
    {
        List<int> c = new List<int>() { 1, 2, 3 };
        List<int> a = new List<int>() { 5, 3, 2, 4 };
        IEnumerable<int> j = c.Union<int>(a);
        // just show me the Count
        Console.Write(j.ToList<int>().Count.ToString());

    }
}

解决方案

You want Intersect():

IEnumerable<int> j = c.Intersect(a);

Here's an OrderedIntersect() example based on the ideas mentioned in the comments. If you know your sequences are ordered it should run faster — O(n) rather than whatever .Intersect() normally is (don't remember off the top of my head). But if you don't know they are ordered, it likely won't return correct results at all:

public static IEnumerable<T> OrderedIntersect<T>(this IEnumerable<T> source, IEnumerable<T> other) where T : IComparable
{
    using (var xe = source.GetEnumerator())
    using (var ye = other.GetEnumerator())
    {
        while (xe.MoveNext())
        {
           while (ye.MoveNext() && ye.Current.CompareTo(xe.Current) < 0 )
           {
              // do nothing - all we care here is that we advanced the y enumerator
           }
           if (ye.Current.Equals(xe.Current))
              yield return xe.Current;
           else
           {  // y is now > x, so get x caught up again
              while (xe.MoveNext() && xe.Current.CompareTo(ye.Current) < 0 )
              { } // again: just advance, do do anything

              if (xe.Current.Equals(ye.Current)) yield return xe.Current;
           }

        }
    }
}