如何在LINQ C#中比较三个列表ID? [英] How to compare three list ID's in LINQ C#?

查看:76
本文介绍了如何在LINQ C#中比较三个列表ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有3个列表,我想比较它的模块。



列表C1 
-------------
ID | MODULE |
------------- |
1 | M1 |
------------- |
2 | M2 |
------------- |
3 | M3 |
--------------


列表C2
-------------
ID | MODULE |
------------- |
1 | M1 |
------------- |
2 | M2 |
------------- |
3 | M4 |
--------------


列表C3
-------------
ID | MODULE |
------------- |
1 | M5 |
------------- |
2 | M6 |
------------- |
3 | M7 |
--------------





我的尝试:



我想将这三个列表相互比较。



如果任何一个值任何表格,匹配,然后我不想拿那个列表。



请帮帮我。



谢谢

解决方案

我不知道如果任何一个表的任何一个值匹配,那么我不想拿那个列表。意思是这里是你如何使用linq比较列表



所以我创建了一个代表你的表的类



  public   class  CodeProject 
{
public int ID { get ; set ; }
public string 模块{ get ; set ; }
}





然后以数据为例,填充列表并与复杂对象进行比较。 />


  var  c1 =  new 列表< codeproject>(); 
var c2 = new 列表< codeproject>();
var c3 = new 列表< codeproject>();

c1.Add( new CodeProject {Id = 1 ,Module = < span class =code-string> M1});
c1.Add( new CodeProject {Id = 2 ,Module = M2});
c1.Add( new CodeProject {Id = 3 ,Module = M3});

c2.Add( new CodeProject {Id = 1 ,Module = < span class =code-string> M1});
c2.Add( new CodeProject {Id = 2 ,Module = M2});
c2.Add( new CodeProject {Id = 3 ,Module = M4});

c3.Add( new CodeProject {Id = 1 ,Module = < span class =code-string> M5});
c3.Add( new CodeProject {Id = 2 ,Module = M6});
c3.Add( new CodeProject {Id = 3 ,Module = M7});

// 获取c1列表中存在的项目
< span class =code-keyword> var similarC1toC2 = c1.Where(m = > c2.Select(x = > x.Module).Contains(m.Module));
var similarC1toC3 = c1.Where(m = > c3.Select(x = < span class =code-keyword>> x.Module).Contains(m.Module));

// 获取c1列表中存在的项目
< span class =code-keyword> var notsimilarC1toC2 = c1.Where(m = > !c2.Select(x = > x.Module).Contains(m.Module));
var notsimilarC1toC3 = c1.Where(m = > !c3.Select(x = > x.Module).Contains(m.Module));

if (similarC1toC2.Any())
{
Console.WriteLine( 列出c1和c2共享值,不要使用);
}

if (similarC1toC3.Any())
{
Console.WriteLine( 列出c1和c3共享值,不要使用);
}
< / codeproject > < / codeproject > ; < / codeproject >





上面的例子显示了列表中的值是相同的。如果你想得到两个列表之间不存在的值,那么使用notsimilar *变量。



希望这就是你要找的东西。


Hi guys,

I have 3 lists and i want to compare its modules.

List C1
-------------
 ID | MODULE |
-------------|
 1  |  M1    |
-------------|
 2  |  M2    |
-------------|
 3  |  M3    |
--------------


List C2
-------------
 ID | MODULE |
-------------|
 1  |  M1    |
-------------|
 2  |  M2    |
-------------|
 3  |  M4    |
--------------


List C3
-------------
 ID | MODULE |
-------------|
 1  |  M5    |
-------------|
 2  |  M6    |
-------------|
 3  |  M7    |
--------------



What I have tried:

I want to compare these three list with one another.

if any one value of any table, is matching with, then i dont want take that list.

Please help me.

Thanks

解决方案

I don't know what "if any one value of any table, is matching with, then i dont want take that list." means but here is how you can compare lists using linq

So i created a class to represent your table

public class CodeProject
{
    public int Id { get; set; }
    public string Module { get; set; }
}



Then using your data as an example, populate the list and do the comparison with complex objects.

var c1 = new List<codeproject>();
            var c2 = new List<codeproject>();
            var c3 = new List<codeproject>();

            c1.Add(new CodeProject { Id = 1, Module = "M1" });
            c1.Add(new CodeProject { Id = 2, Module = "M2" });
            c1.Add(new CodeProject { Id = 3, Module = "M3" });

            c2.Add(new CodeProject { Id = 1, Module = "M1" });
            c2.Add(new CodeProject { Id = 2, Module = "M2" });
            c2.Add(new CodeProject { Id = 3, Module = "M4" });

            c3.Add(new CodeProject { Id = 1, Module = "M5" });
            c3.Add(new CodeProject { Id = 2, Module = "M6" });
            c3.Add(new CodeProject { Id = 3, Module = "M7" });

            // Get items that exist in c1 list
            var similarC1toC2 = c1.Where(m => c2.Select(x => x.Module).Contains(m.Module));
            var similarC1toC3 = c1.Where(m => c3.Select(x => x.Module).Contains(m.Module));

// Get items that exist in c1 list
            var notsimilarC1toC2 = c1.Where(m => !c2.Select(x => x.Module).Contains(m.Module));
            var notsimilarC1toC3 = c1.Where(m => !c3.Select(x => x.Module).Contains(m.Module));

            if (similarC1toC2.Any())
            {
                Console.WriteLine("List c1 and c2 share values, don't use");
            }

            if (similarC1toC3.Any())
            {
                Console.WriteLine("List c1 and c3 share values, don't use");
            }
</codeproject></codeproject></codeproject>



The example above shows what values from the list are the same. If you wanted to get what values don't exist between the two lists then use the notsimilar* variables.

Hope that is what you are looking for.


这篇关于如何在LINQ C#中比较三个列表ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆