针对数组可以用List.Distinct(),可以过滤掉重复的内容。

针对对象中的某个字段只能用Distinct(IEqualityComparer<T>)

用法:

58    }
 
 

同样table 也可以用这个方法。只要一步ASEnumerable()即可

 
 
            var _comPresult = _dt.AsEnumerable().Distinct(new DataTableRowCompare());
            DataTable _resultDt = _comPresult.CopyToDataTable();
 
            _resultDt.AsEnumerable().ToList().ForEach(
               x =>
               {
                   Console.WriteLine(x["id"].ToString() + "    " + x["name"].ToString() + "   " + x["address"].ToString());
               });
 
 
public class DataTableRowCompare : IEqualityComparer<DataRow>
    {
 
        #region IEqualityComparer<DataRow> 成员
 
        public bool Equals(DataRow x, DataRow y)
        {
            return (x.Field<int>("id") == y.Field<int>("id")); 这个是根据自己的需求写比较字段的
        }
 
        public int GetHashCode(DataRow obj)
        {
            return obj.ToString().GetHashCode();
        }
 
        #endregion
    } 
 

相关文章:

  • 2021-06-13
  • 2022-12-23
  • 2021-09-02
  • 2021-09-18
  • 2022-12-23
  • 2021-09-10
  • 2022-01-16
猜你喜欢
  • 2021-05-20
  • 2022-12-23
  • 2022-02-25
  • 2021-06-18
  • 2022-12-23
相关资源
相似解决方案