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
C# 使用lambda表达式查找两个列表之间的公共元素_C#_Linq_Lambda - Fatal编程技术网

C# 使用lambda表达式查找两个列表之间的公共元素

C# 使用lambda表达式查找两个列表之间的公共元素,c#,linq,lambda,C#,Linq,Lambda,我有以下两个清单: var firstList = new List<ProgramInfo> () { new ProgramInfo {Name = "A", ProgramId = 1, Description = "some text1"}, new ProgramInfo {Name = "C", ProgramId = 2, Description = "some text2"}, new ProgramInfo {Name = "D", Prog

我有以下两个清单:

var firstList = new List<ProgramInfo> ()
{
    new ProgramInfo {Name = "A", ProgramId = 1, Description = "some text1"},
    new ProgramInfo {Name = "C", ProgramId = 2, Description = "some text2"},
    new ProgramInfo {Name = "D", ProgramId = 3, Description = "some text3"},
    new ProgramInfo {Name = "E", ProgramId = 4, Description = "some text4"}
};

var secondList = new List<ProgramInfo> ()
{
    new ProgramInfo {Name = "C", ProgramId = 2, Description = "some text1"},
    new ProgramInfo {Name = "D", ProgramId = 3, Description = "some text2"},
};
var firstList=新列表()
{
新程序信息{Name=“A”,ProgramId=1,Description=“some text1”},
新程序信息{Name=“C”,ProgramId=2,Description=“some text2”},
新程序信息{Name=“D”,ProgramId=3,Description=“some text3”},
新程序信息{Name=“E”,ProgramId=4,Description=“some text4”}
};
var secondList=新列表()
{
新程序信息{Name=“C”,ProgramId=2,Description=“some text1”},
新程序信息{Name=“D”,ProgramId=3,Description=“some text2”},
};
这两个列表是在运行时生成的,我必须根据这两个列表中的程序id选择公共ProgramInfo

例如,在上述示例中,输出应为

var thirdList = new List<ProgramInfo>()
{
    new ProgramInfo {Name = "C", ProgramId = 2, Description = "some text1"},
    new ProgramInfo {Name = "D", ProgramId = 3, Description = "some text2"},
};

  public class ProgramInfo
  {
    public string Name { get; set; }
    public int ProgramId { get; set; }
    public string Description { get; set; }
  }
var thirdList=新列表()
{
新程序信息{Name=“C”,ProgramId=2,Description=“some text1”},
新程序信息{Name=“D”,ProgramId=3,Description=“some text2”},
};
公共类程序信息
{
公共字符串名称{get;set;}
公共int程序ID{get;set;}
公共字符串说明{get;set;}
}

有人能建议我如何使用lambda表达式来执行此操作吗?

使用Linq
.Intersect
。为此,您的类需要重写
Equals
GetHashCode

var thirdList = firstList.Intersect(secondList);
您还可以指定一个
IEqualityComparer
,而不是覆盖函数:

public class Comparer : IEqualityComparer<ProgramInfo>
{
   public bool Equals(ProgramInfo x, ProgramInfo y)
   {
      return x.Name == y.Name &&
             x.ProgramId == y.ProgramId &&
             x.Description == y.Description;
   }

   public int GetHashCode(ProgramInfo obj)
   {
      return obj.Name.GetHashCode() ^
             obj.ProgramId.GetHashCode() ^
             obj.Description.GetHashCode();
   }
}

var thirdList = firstList.Intersect(secondList, new Comparer());
公共类比较器:IEqualityComparer
{
公共布尔等于(ProgramInfo x,ProgramInfo y)
{
返回x.Name==y.Name&&
x、 ProgramId==y.ProgramId&&
x、 描述==y.描述;
}
公共int GetHashCode(ProgramInfo obj)
{
返回obj.Name.GetHashCode()^
obj.ProgramId.GetHashCode()^
obj.Description.GetHashCode();
}
}
var thirdList=firstList.Intersect(secondList,new Comparer());

使用Linq
.Intersect
。为此,您的类需要重写
Equals
GetHashCode

var thirdList = firstList.Intersect(secondList);
您还可以指定一个
IEqualityComparer
,而不是覆盖函数:

public class Comparer : IEqualityComparer<ProgramInfo>
{
   public bool Equals(ProgramInfo x, ProgramInfo y)
   {
      return x.Name == y.Name &&
             x.ProgramId == y.ProgramId &&
             x.Description == y.Description;
   }

   public int GetHashCode(ProgramInfo obj)
   {
      return obj.Name.GetHashCode() ^
             obj.ProgramId.GetHashCode() ^
             obj.Description.GetHashCode();
   }
}

var thirdList = firstList.Intersect(secondList, new Comparer());
公共类比较器:IEqualityComparer
{
公共布尔等于(ProgramInfo x,ProgramInfo y)
{
返回x.Name==y.Name&&
x、 ProgramId==y.ProgramId&&
x、 描述==y.描述;
}
公共int GetHashCode(ProgramInfo obj)
{
返回obj.Name.GetHashCode()^
obj.ProgramId.GetHashCode()^
obj.Description.GetHashCode();
}
}
var thirdList=firstList.Intersect(secondList,new Comparer());

这可能是一个输入错误,但不确定-我是说
第二个列表
第三个列表
你是说
“一些文本2”
“一些文本3”
就像上面的列表一样?我不确定我在这里看到了多少研究成果…这可能是一个输入错误,但不确定-我是说
第二个列表
第三个列表
你是指
“一些TeT2”/代码>和<代码>一些TeX3“<代码>类似于上面的列表吗?我不确定我在这里看到了很多研究工作……@ App-酷:如果你发现这有帮助,请考虑标记为解决和投票:)@ APP -酷:如果你发现这有帮助,请考虑标记为解决和投票: