尝试删除数据表中的重复记录时出错 [英] Error when trying to delete duplicate records in a datatable

查看:76
本文介绍了尝试删除数据表中的重复记录时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下错误

错误 1 'System.Data.EnumerableRowCollection< System.Data.DataRow>'不包含"Distinct"的定义,也没有扩展方法"Distinct"接受类型为"System.Data.EnumerableRowCollection< System.Data.DataRow>"的第一个参数可以找到 (您是否缺少using指令或程序集引用?) C:\ Users \ Balasundar.SP \ Documents \ Visual Studio 2013 \ Projects \ fire \ Ingestion \ IngestData.cs 643 88 摄取

Error 1 'System.Data.EnumerableRowCollection<System.Data.DataRow>' does not contain a definition for 'Distinct' and no extension method 'Distinct' accepting a first argument of type 'System.Data.EnumerableRowCollection<System.Data.DataRow>' could be found (are you missing a using directive or an assembly reference?) C:\Users\Balasundar.SP\Documents\Visual Studio 2013\Projects\fire\Ingestion\IngestData.cs 643 88 Ingestion

这是代码

DatarowduplicateEliminator类:IEqualityComparer< DataRow>
    {
       私人string []键=新string [] {"item","PriceDateTime" };
           公共布尔值等于(DataRow x,DataRow y)
            {
               布尔标志=假; //默认不一样
                foreach(键中的变量项)
                {
                   标志=(x [item] .ToString()== y [item] .ToString());
                }
               返回标志;
            }
            public int GetHashCode(DataRow obj)
            {
               返回obj ["item"].ToString().GetHashCode();
            }
    }

class DatarowduplicateEliminator : IEqualityComparer<DataRow>
    {
        private string[] keys = new string[] { "item", "PriceDateTime" };
            public bool Equals(DataRow x, DataRow y)
            {
                bool flag = false; //Default isn't the same
                foreach (var item in keys)
                {
                    flag = (x[item].ToString() == y[item].ToString());
                }
                return flag;
            }
            public int GetHashCode(DataRow obj)
            {
                return obj["item"].ToString().GetHashCode();
            }
    }

我正在按如下方式调用该方法

I am calling the method as follows

var dtFinal = dt.AsEnumerable().Distinct(new DatarowduplicateEliminator());

var dtFinal = dt.AsEnumerable().Distinct(new DatarowduplicateEliminator());

我添加的using指令如下

The using directives i have added are as follows

使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Collections.Specialized;
使用System.Configuration;
使用System.Data;
使用System.Data.SqlClient;
使用System.Globalization;
使用System.IO;
使用System.Reflection;
使用System.Runtime.Serialization.Formatters.Binary;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

有人可以帮助我确定问题吗?

Can somebody help me identify the problem?

谢谢

巴拉苏达(Balasundar)

Balasundar


推荐答案

包含LINQ:

Include LINQ:

using System.Linq;


这篇关于尝试删除数据表中的重复记录时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆