Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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#_List_Linq_Data Structures - Fatal编程技术网

C# 除了从创建新列表的第三个属性中获得唯一值列表外,还可以通过两个属性比较两个列表

C# 除了从创建新列表的第三个属性中获得唯一值列表外,还可以通过两个属性比较两个列表,c#,list,linq,data-structures,C#,List,Linq,Data Structures,假设我有两个集合示例Collection1和示例Collection2。 我想通过linq对两个集合中的属性“ab”和“gh”进行迭代,并为xy创建一个新列表。我不担心形象 var ex1List = new List<ExampleCollection1> { new ExampleCollection1 {ab = "10", gh = "200" }, new ExampleCollection1 {ab = "10", gh = "300" },

假设我有两个集合示例Collection1和示例Collection2。 我想通过linq对两个集合中的属性“ab”和“gh”进行迭代,并为xy创建一个新列表。我不担心形象

 var ex1List = new List<ExampleCollection1>
 {
    new ExampleCollection1 {ab = "10", gh = "200" },
    new ExampleCollection1 {ab = "10", gh = "300" },
    new ExampleCollection1 {ab = "10", gh = "400" },
    new ExampleCollection1 {ab = "10", gh = "500" },
    new ExampleCollection1 {ab = "10", gh = "600" }
 };

   var ex2List = new List<ExampleCollection2>
   {
    new ExampleCollection2 {ab = "10", gh = "200", xy = "5", image = "star"},
    new ExampleCollection2 {ab = "10", gh = "200", xy = "10", image = "moon"},
    new ExampleCollection2 {ab = "10", gh = "400", xy = "4", image = "globe" },
    new ExampleCollection2 {ab = "10", gh = "400", xy = "4", image = "earth" },//this should not be in the resulting collection as previous entry and this are same for ab, gh, and xy
    new ExampleCollection2 {ab = "10", gh = "600", xy = "3", image = "twinkle" }
   };
var ex1List=新列表
{
新示例集合1{ab=“10”,gh=“200”},
新示例集合1{ab=“10”,gh=“300”},
新示例集合1{ab=“10”,gh=“400”},
新示例集合1{ab=“10”,gh=“500”},
新示例集合1{ab=“10”,gh=“600”}
};
var ex2List=新列表
{
新示例集合2{ab=“10”,gh=“200”,xy=“5”,image=“star”},
新示例集合2{ab=“10”,gh=“200”,xy=“10”,image=“moon”},
新示例集合2{ab=“10”,gh=“400”,xy=“4”,image=“globe”},
new ExampleCollection2{ab=“10”,gh=“400”,xy=“4”,image=“earth”},//这不应该像前面的条目一样出现在结果集合中,这与ab、gh和xy相同
新示例集合2{ab=“10”,gh=“600”,xy=“3”,image=“闪烁”}
};
所以,我需要最后的列表来包含

var finalList = new List<ExampleCollection2>
            {
                new ExampleCollection2 {ab = "10", gh = "200", xy = "5", image = "star"},
                new ExampleCollection2 {ab = "10", gh = "200", xy = "10", image = "moon"},
                new ExampleCollection2 {ab = "10", gh = "400", xy = "4", image = "globe" },
                new ExampleCollection2 {ab = "10", gh = "600", xy = "3", image = "twinkle" }
            };
var finalList=新列表
{
新示例集合2{ab=“10”,gh=“200”,xy=“5”,image=“star”},
新示例集合2{ab=“10”,gh=“200”,xy=“10”,image=“moon”},
新示例集合2{ab=“10”,gh=“400”,xy=“4”,image=“globe”},
新示例集合2{ab=“10”,gh=“600”,xy=“3”,image=“闪烁”}
};
我试过了,但我知道它不完整。需要一种方法,在这里我可以迭代并获得xy的不同值

 foreach (var item in ex1List)
            {
                var finalList = ex2List?.FindAll(val => val.ab != null && val.gh != null &&
                    CheckEquals(item.ab, item.gh, val.ab, val.gh)); // here need  to check for distinct values for xy too. how to do that ?

                foreach(var x in finalList)
                {
                    Console.WriteLine(x.ab + x.gh + x.xy + x.image);
                }
            }

private bool CheckEquals(<...>)
{
...
}
foreach(ex1List中的变量项)
{
var finalList=ex2List?.FindAll(val=>val.ab!=null&&val.gh!=null&&
CheckEquals(item.ab,item.gh,val.ab,val.gh));//这里还需要检查xy的不同值。如何操作?
foreach(finalList中的变量x)
{
控制台写入线(x.ab+x.gh+x.xy+x.image);
}
}
私有布尔CheckEquals()
{
...
}

这相当容易。试试这个:

var finalList =
(
    from ex1 in ex1List
    join ex2 in ex2List on new { ex1.ab, ex1.gh } equals new { ex2.ab, ex2.gh }
    group ex2 by ex2.xy into gex2s
    from gex2 in gex2s.Take(1)
    select gex2
).ToList();
这给了我:


这取决于
ex2List
的顺序,以排除
earth
超过
globe
。您可能需要包含一个
orderby
,以确保确定的选择。

谢谢@Enigmativity。我可以试试这个,但我想用我的CheckEquals函数来检查ab和gh,因为我把它们作为字符串,例如,但它们是自定义类型,所以当使用CheckEquals时,这将如何改变???@jamilia-请不要在答案的注释中改变你的问题的要求。请编辑您的问题以澄清此新要求。另外,请提供一个完整的
bool CheckEquals()
实现,该实现与您在问题中提出的要求相匹配。此外,请包括您的类型定义。