数据表 - Linq [英] Data table - Linq

查看:71
本文介绍了数据表 - Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个12列的数据表。其中5个一起制作主键。我想在列中找到重复的行和赋值'DUP'(例如,为此目的保留的最后一列)所有这些重复记录

我遍历数据表行并完成了更新。但要求是在linq中编码..请帮助..在linq中尝试了一些代码但是无法获得所需的结果.....因为我有5列作为复合主键。请帮助..

谢谢..

Hi,
I have a datatable with 12 columns. 5 of them together makes primary key. I want to find the duplicate rows and assign value 'DUP' in a column (say, last column which is kept for this purpose) of all those duplicate records
I iterated through rows of datatable and have done the updation. but the requirement is to code in linq.. please help.. Tried some code in linq but could nt get the desired result..... because i ve 5 columns as composite primary key. please help..
Thank you..

推荐答案

这是一个链接,您可能会从以下方面获得一些想法: http://stackoverflow.com/questions/10984453/compare-two-datatables-for-differences-in- c [ ^ ]



或者,如果你想更加冗长和繁琐:



var query1 =来自_Context.table1中的A选择新的{A.fieldOne,A.fieldTwo,A.fieldThree,A.fieldFour,A.fieldFive};

IEnumerable TableOne = query1.ToList();



此时,您将表1作为要比较的所有关键字段的列表。

_Context是您的数据上下文,您可以通过实例化您的数据模型/实体来建立

然后:



var query2 =来自_Context.table2中的A选择新的{A.fieldOne,A.fieldTwo,A.fieldThree,A.fieldFour,A.fieldFive};

IEnumerable TableTwo = query2.ToList() ;



此时你可以比较两者(TableOne和TableTwo)
Here is a link you might get some ideas from: http://stackoverflow.com/questions/10984453/compare-two-datatables-for-differences-in-c[^]

Or, if you want to do it more verbosely and cumbersome:

var query1 = from A in _Context.table1 select new { A.fieldOne, A.fieldTwo, A.fieldThree,A.fieldFour,A.fieldFive };
IEnumerable TableOne= query1.ToList();

At this point you have Table 1 as a list of all the key fields you want to compare.
_Context is your data context, which you can establish by instantiating your data model/entities
Then:

var query2 = from A in _Context.table2 select new { A.fieldOne, A.fieldTwo, A.fieldThree,A.fieldFour,A.fieldFive };
IEnumerable TableTwo= query2.ToList();

At this point you could be able compare the two (TableOne and TableTwo)


    //I have a class called tmpdata that looks like this
    public class tmpdata
    {
        public tmpdata() { }
        public tmpdata(int c1, int c2, int c3, int c4, int c5)
        {
            this.col1 = c1;
            this.col2 = c2;
            this.col3 = c3;
            this.col4 = c4;
            this.col5 = c5;
        }
        public int col1;
        public int col2;
        public int col3;
        public int col4;
        public int col5;
        public string isDup;
    }

//Data population into tmpdata List
            List<tmpdata> data = new List<tmpdata>();

            tmpdata d1 = new tmpdata(1, 2, 3, 4, 5); //Duplicate Record
            tmpdata d2 = new tmpdata(1, 3, 4, 5, 6);
            tmpdata d3 = new tmpdata(1, 2, 3, 4, 5); //Duplicate Record
            tmpdata d4 = new tmpdata(4, 3, 2, 1, 5);
            tmpdata d5 = new tmpdata(1, 5, 3, 4, 2);
            tmpdata d6 = new tmpdata(2, 3, 4, 5, 1);

            data.Add(d1);
            data.Add(d2);
            data.Add(d3);
            data.Add(d4);
            data.Add(d5);
            data.Add(d6);

            //Finding Duplicate rows with LINQ Query
            var duplicateRows = from x in data
                                group x by new { x.col1, x.col2, x.col3, x.col4, x.col5 } into g
                                let ct = g.Count()
                                orderby ct
                                where ct > 1
                                select new tmpdata()
                                {
                                    col1 = g.Key.col1,
                                    col2 = g.Key.col2,
                                    col3 = g.Key.col3,
                                    col4 = g.Key.col4,
                                    col5 = g.Key.col5,
                                };
            //Now you can enumerate to Mark Duplicate records


这篇关于数据表 - Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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