C# 查找列表中与其第一项相比的不同项
我有一个C# 查找列表中与其第一项相比的不同项,c#,linq,C#,Linq,我有一个GroupSummary类,其中有一些类似的属性: public class GroupsSummary { public bool UsedRow { get; set; } public string GroupTin { get; set; } public string PayToZip_4 { get; set; } public string PayToName { get; set; } public string PayT
GroupSummary
类
,其中有一些类似的属性:
public class GroupsSummary
{
public bool UsedRow { get; set; }
public string GroupTin { get; set; }
public string PayToZip_4 { get; set; }
public string PayToName { get; set; }
public string PayToStr1 { get; set; }
public string PayToStr2 { get; set; }
public string PayToCity { get; set; }
public string PayToState { get; set; }
public bool UrgentCare_YN { get; set; }
}
然后我有一本
字典
像你可以试试这样的东西:
List<GroupSummary> result = dictionary[key].Skip(1)
.Where(x=>(x.PayToStr1+
x.PayToStr2+
x.PayToCity+
x.PayToState)!=
(dictionary[key][0].PayToStr1+
dictionary[key][0].PayToStr2+
dictionary[key][0].PayToCity+
dictionary[key][0].PayToState))
.ToList();
List result=dictionary[key]。跳过(1)
.其中(x=>(x.PayToStr1+
x、 付款方式TR2+
x、 支付能力+
x、 付费状态)=
(字典[键][0]。付款方式TR1+
字典[key][0]。付款方式TR2+
字典[key][0]。付费城市+
字典[key][0]。支付状态)
.ToList();
您可以尝试以下方法:
List<GroupSummary> result = dictionary[key].Skip(1)
.Where(x=>(x.PayToStr1+
x.PayToStr2+
x.PayToCity+
x.PayToState)!=
(dictionary[key][0].PayToStr1+
dictionary[key][0].PayToStr2+
dictionary[key][0].PayToCity+
dictionary[key][0].PayToState))
.ToList();
List result=dictionary[key]。跳过(1)
.其中(x=>(x.PayToStr1+
x、 付款方式TR2+
x、 支付能力+
x、 付费状态)=
(字典[键][0]。付款方式TR1+
字典[key][0]。付款方式TR2+
字典[key][0]。付费城市+
字典[key][0]。支付状态)
.ToList();
您的问题:
查找列表中与列表中第一项不同的项
下面是一个简单整数列表的解决方案:
var items=新列表{1,2,3,1,7,4,6,1,9};
var查询=项目
.Skip(1)
.Distinct()
.Where(x=>x!=items.First())
.OrderBy(x=>x);
foreach(查询中的int项)
{
控制台写入线(项目);
}
预期输出:
2
3.
4.
6.
7.
9
扩展此查询以使用GroupsSummary
类很容易。只需定义一个实例方法(如果您不拥有GroupsSummary
类,则定义一个扩展方法)来检查两个GroupsSummary
对象的“等价性”:
public bool IsEquivalentTo(GroupsSummary other)
{
return
this.PayToStr1.Equals(other.PayToStr1) &&
this.PayToStr2.Equals(other.PayToStr2) &&
this.PayToCity.Equals(other.PayToCity) &&
this.PayToState.Equals(other.PayToState);
}
上面LINQ查询中的Where
约束简单地变成:
.Where(x => !x.IsEquivalentTo(items.First())
您的问题:
查找列表中与列表中第一项不同的项
下面是一个简单整数列表的解决方案:
var items=新列表{1,2,3,1,7,4,6,1,9};
var查询=项目
.Skip(1)
.Distinct()
.Where(x=>x!=items.First())
.OrderBy(x=>x);
foreach(查询中的int项)
{
控制台写入线(项目);
}
预期输出:
2
3.
4.
6.
7.
9
扩展此查询以使用GroupsSummary
类很容易。只需定义一个实例方法(如果您不拥有GroupsSummary
类,则定义一个扩展方法)来检查两个GroupsSummary
对象的“等价性”:
public bool IsEquivalentTo(GroupsSummary other)
{
return
this.PayToStr1.Equals(other.PayToStr1) &&
this.PayToStr2.Equals(other.PayToStr2) &&
this.PayToCity.Equals(other.PayToCity) &&
this.PayToState.Equals(other.PayToState);
}
上面LINQ查询中的Where
约束简单地变成:
.Where(x => !x.IsEquivalentTo(items.First())
当然是第二个建议——如果您的条件发生变化,您希望在一个地方更改代码。(即使在无法编辑GroupsSummary
的情况下,也可以使用扩展方法执行相同的操作。)例如“.Where(first.isdifferentadresfrom(i))”?它不识别iOh,我认为它应该是:“var others=from I in items.Skip(1)。Where(t=>t.IsDifferentAddressFrom(first))选择I;”我在想Where I.IsDifferentAddressFrom(first)选择I代码>当然是第二个建议——如果您的情况发生变化,您希望在一个地方更改代码。(即使在无法编辑GroupsSummary
的情况下,也可以使用扩展方法执行相同的操作。)例如“.Where(first.isdifferentadresfrom(i))”?它不识别iOh,我认为它应该是:“var others=from I in items.Skip(1)。Where(t=>t.IsDifferentAddressFrom(first))选择I;”我在想Where I.IsDifferentAddressFrom(first)选择I代码>