Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# C-查找两个Lists-Lambda语法的公共成员_C#_.net_Linq_List_Lambda - Fatal编程技术网

C# C-查找两个Lists-Lambda语法的公共成员

C# C-查找两个Lists-Lambda语法的公共成员,c#,.net,linq,list,lambda,C#,.net,Linq,List,Lambda,所以我编写了这个简单的控制台应用程序来帮助我提问。使用方法第3行上的lambda表达式获取公共成员的正确方法是什么。尝试了联接,但无法找出正确的语法。作为后续行动。。。有没有一种非LINQ的方法可以在我错过的一行中做到这一点 class Program { static void Main(string[] args) { List<int> c = new List<int>() { 1, 2, 3 }; List<

所以我编写了这个简单的控制台应用程序来帮助我提问。使用方法第3行上的lambda表达式获取公共成员的正确方法是什么。尝试了联接,但无法找出正确的语法。作为后续行动。。。有没有一种非LINQ的方法可以在我错过的一行中做到这一点

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());

    }
}
你想要交集:


如果lambda语法指的是一个真正的LINQ查询,则如下所示:

IEnumerable<int> j =
   from cItem in c
   join aitem in a on cItem equals aItem
   select aItem;
Dictaionary<int, bool> match = new Dictaionary<int, bool>();
foreach (int i in c) match.Add(i, false);
foreach (int i in a) {
   if (match.ContainsKey(i)) {
      match[i] = true;
   }
}
List<int> result = new List<int>();
foreach (KeyValuePair<int,bool> pair in match) {
   if (pair.Value) result.Add(pair.Key);
}
lambda表达式是在使用=>运算符时使用的,如:

IEnumerable<int> x = a.Select(y => y > 5);
Union方法实际上是一种非LINQ的方法,但我想你指的是一种没有扩展方法的方法。这方面几乎没有一条班轮。昨天我用字典做了类似的事情。你可以这样做:

IEnumerable<int> j =
   from cItem in c
   join aitem in a on cItem equals aItem
   select aItem;
Dictaionary<int, bool> match = new Dictaionary<int, bool>();
foreach (int i in c) match.Add(i, false);
foreach (int i in a) {
   if (match.ContainsKey(i)) {
      match[i] = true;
   }
}
List<int> result = new List<int>();
foreach (KeyValuePair<int,bool> pair in match) {
   if (pair.Value) result.Add(pair.Key);
}

列表是按规范排序的,还是恰好是您的测试用例?这将影响解决方案。如果它们没有被排序,你应该改变你的例子。在一行中没有非LINQ的方法可以做到这一点。检查这个问题,了解完成这个任务的其他方法:好的观点。假设它们不会被分类。现在修复。不过,您在订购方面做得很好-如果对它们进行排序,Zip变体将更有效。横断想不出正确的关键字+1和答案。谢谢