Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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#_Linq - Fatal编程技术网

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