自定义数组比较或查找在另一个数组中不存在的值
本文关键字:数组 不存在 另一个 比较 查找 自定义 | 更新日期: 2023-09-27 18:12:07
我有两个自定义类,
-
BeneficiaryResponse
和 -
BeneficiaryDetail
都有key Id
和AliasName
字段。
我想在Linq查询的帮助下,从第一类中找到那些缺失的值,即受益人响应,这在受益人详细信息中不存在。如。受益人响应有30个参数,受益人有20个参数,然后我想要从受益人响应中缺少的10个参数。
public class BeneficiaryDetail
{
public BeneficiaryDetail[] beneInternal
{
get;
set;
}
public int? Id
{
get;
set;
}
public string AliasName
{
get;
set;
}
}
public class BeneficiaryResponse
{
public int Id
{
get;
set;
}
public string AliasName
{
get;
set;
}
}
现在受益人响应有以下内容,
[{" Id ": 763886年,"AliasName":"abc - 3594"},
{" Id ": 763759,"AliasName":"abc - 6789"},
{" Id ": 763661,"AliasName":"abc - 9111"},
{" Id ": 763668,"AliasName":"abc - 9111"},
{" Id ": 764467,"AliasName":"abc - 3594"}]
和BeneficiaryDetail有
[{" Id ": 763886年,"AliasName":"abc - 3594"},
{" Id ": 763759,"AliasName":"abc - 6789"},
{" Id ": 764467,"AliasName":"abc - 3594"}]
如何从BeneficiaryResponse中获得2个缺失的其余部分
添加Newtonsoft。Josn dll
public void Main(string[] args)
{
string strBR = "[{ 'Id':763886,'AliasName':'ABC-3594'},{ 'Id':763759,'AliasName':'ABC-6789'},{ 'Id':763661,'AliasName':'ABC-9111'},{ 'Id':763668,'AliasName':'ABC-9111'},{ 'Id':764467,'AliasName':'ABC-3594'}]";
string strBD = " [{'Id':763886,'AliasName':'ABC-3594'},{'Id':763759,'AliasName':'ABC-6789'},{'Id':764467,'AliasName':'ABC-3594'}]";
var resultBR = JsonConvert.DeserializeObject<List<BeneficiaryResponse>>(strBR);
var resultBD = JsonConvert.DeserializeObject<List<BeneficiaryDetail>>(strBD);
var missingData = resultBR.Where(x => !resultBD.Select(n => n.Id).Contains(x.Id)).ToList();
foreach (var item in missingData)
{
Console.WriteLine(item.Id);
}
}
public class BeneficiaryResponse
{
public int Id { get; set; }
public string AliasName { get; set; }
}
public class BeneficiaryDetail
{
public int Id { get; set; }
public string AliasName { get; set; }
}
输出:763661 763668
您可以使用以下linq代码进行输出。它将根据ID参数给出输出。这可能对你有帮助。
var result= (from c in BeneficiaryResponse
where !(from o in BeneficiaryDetail select o.ID).Contains(c.ID)
select c);
或
var result= from BResponse in BeneficiaryResponse
where !(BeneficiaryDetail.Any
(BDetail => BDetail.ID == Response.ID
&& BDetail.AliasName == BResponse.AliasName))
select BResponse;