Home  >  Article  >  Backend Development  >  C# uses Linq to implement DataTable to implement duplicate data filtering

C# uses Linq to implement DataTable to implement duplicate data filtering

黄舟
黄舟Original
2017-02-18 10:21:042602browse

Before filtering:


##After filtering(only filtered out Exactly the same data) :


##The code is as follows:

##

DataTable dt = this.JsonToDataTable("[{\"Code\":\"SortId\",\"Name\":\"SortId\"},{\"Code\":\"SortCode\",\"Name\":\"编号\"},
{\"Code\":\"SolutionName\",\"Name\":\"名称\"},{\"Code\":\"SortId\",\"Name\":\"SortId\"},{\"Code\":\"SortId\",\"Name\":\"SortId\"},
{\"Code\":\"SortId001\",\"Name\":\"SortId\"}]");
            DataTable dtSort = dt.Clone();
            var query = from t in dt.AsEnumerable()
                        group t by new { t1 = t.Field<string>("Code"),t2 = t.Field<string>("Name") } into m
                        select new
                        {
                            code = m.Key.t1,
                            name=m.Key.t2,
                            rowcount = m.Count()
                        };
            if (query.ToList().Count > 0)
            {
                query.ToList().ForEach(q =>
                {
                    DataRow dr = dtSort.NewRow();
                    dr["Code"] = q.code;
                    dr["Name"] = q.name;
                    dtSort.Rows.Add(dr);  
                });
            }
The above is the content of C# using Linq to implement DataTable to implement duplicate data filtering. For more related content, please pay attention to PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn