Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..
Posted
Comments
Thanks7872 25-Aug-13 7:17am    
Tried some code in linq but could nt get the desired result

Then you should post it here in order to let us know what you mean by desired results. And also if there is any mistake,we can point it.

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)
 
Share this answer
 
    //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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900