3

我有一个有一些重复的列表。

Row#    Lineid  ItemDescItemId  RoadTax VehicleId   Amount
1   122317  None    -1  26.63   -78603  300
2   122317  None    -2  17.75   -78603  200
3   122317  None    -1  22.19   -78602  250
4   122317  Deli    -2  17.75   -78603  200

在这种情况下,第 2 行是第 4 行的副本,因为 LineId、RoadTax、Amount 和 VehicleId 匹配。但是,我想保留带有项目描述的行并消除第 2 行。所以我的输出列表如下所示:

Row#    Lineid  ItemDesc ItemId RoadTax VehicleId   Amount
1   122317  None    -1  26.63   -78603  300
3   122317  None    -1  22.19   -78602  250
4   122317  Deli    -2  17.75   -78603  200

我根据 MSDN 上的示例编写了一个IEqualityComparer类。该类如下所示:

  public class RoadTaxComparer : IEqualityComparer<RoadTaxDto>
        {
            // Items are equal if ItemId / VehicleId / RoadTax are equal.
            public bool Equals(RoadTaxDto x, RoadTaxDto y)
            {

                //Check whether the compared objects reference the same data. 
                if (Object.ReferenceEquals(x, y)) return true;

                //Check whether any of the compared objects is null. 
                if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                    return false;

                //Check whether the products' properties are equal. 
                return x.VehicleId == y.VehicleId && x.ItemId == y.ItemId && x.RoadTax == y.RoadTax && x.Amount == y.Amount;
            }

            // If Equals() returns true for a pair of objects  
            // then GetHashCode() must return the same value for these objects. 

            public int GetHashCode(RoadTaxDto roadTaxDto)
            {
                //Check whether the object is null 
                if (Object.ReferenceEquals(roadTaxDto, null)) return 0;

                //Get hash code for the VehicleId. 
                int hashVehicleId = roadTaxDto.VehicleId.GetHashCode();

                //Get hash code for the ItemId field. 
                int hashCodeItemId = roadTaxDto.ItemId.GetHashCode();

                //Calculate the hash code for the QuoteTaxDto. 
                return hashVehicleId ^ hashCodeItemId;
            }

        }

RoadTaxDto 结构如下所示:

class RoadTaxDto
{
public int LineId {get;set}
public string ItemDesc {get;set;}
public int VehicleId {get;set;}
public decimal RoadTax {get;set;}
public int VehicleId {get;set;}
public decimal Amount {get;set;}
}

我使用以下命令来消除重复项。

List<RoadTaxDto> mergedList = RoadTaxes.Union(RoadTaxes, new RoadTaxComparer()).ToList();

当我在其上运行比较器时,我不能保证第 2 行被消除。那么如何确保如果一条记录有重复项,则显示“无”的记录将始终从列表中删除。

4

2 回答 2

1

我会将 GetHashCode() 移至 RoadTaxDto,然后执行以下操作:

foreach (var g in list.GroupBy(i => i.GetHashCode()))
    list2.Add(
        g.FirstOrDefault(i => i.ItemDesc != "None") ?? 
        g.First());
于 2013-08-02T17:11:54.357 回答
0

纯粹的“SQLish”方法不适合您吗?

像这样的东西:

var list = new [] { 
    new RoadTaxDto {LineId=122317,ItemDesc="None", ItemId=-1,RoadTax =26.63M  , VehicleId=-78603  ,Amount=300},
    new RoadTaxDto {LineId=122317,ItemDesc="None", ItemId=-2,RoadTax =17.75M  , VehicleId=-78603  ,Amount=200},
    new RoadTaxDto {LineId=122317,ItemDesc="None", ItemId=-1,RoadTax =22.19M  , VehicleId=-78602  ,Amount=250},
    new RoadTaxDto {LineId=122317,ItemDesc="Deli", ItemId=-2,RoadTax =17.75M , VehicleId=-78603  ,Amount=200}
};

var query = (from c in list join x in list 
    on new { c.LineId, c.ItemId , c.VehicleId, c.Amount,c.RoadTax} 
equals new {x.LineId, x.ItemId, x.VehicleId,x.Amount,x.RoadTax}
select new RoadTaxDto {
   LineId = c.LineId,
   ItemDesc = x.ItemDesc!="None"? x.ItemDesc:c.ItemDesc,
   VehicleId=c.VehicleId,
   Amount=c.Amount,
   RoadTax=c.RoadTax,
   ItemId=c.ItemId
}
).GroupBy(x => new { x.LineId, x.RoadTax, x.Amount, x.VehicleId} )
 .Select(grp => grp.Last());

印刷:

LineId  ItemDesc    VehicleId   ItemId  RoadTax Amount
122317  None        -78603      -1      26.63   300
122317  Deli        -78603      -2      17.75   200
122317  None        -78602      -1      22.19   250
于 2013-08-02T16:33:56.563 回答