1

DataSet oDs = new DataSet(); DataTable odt = new DataTable();

        odt.Columns.Add(new DataColumn("FILE_ID", typeof(string)));
        odt.Columns.Add(new DataColumn("ID", typeof(string)));
        oDs.Tables.Add(odt);
        oDs.AcceptChanges();

        for (int i = 1; i < 3; i++)
        {
            DataRow oDr = oDs.Tables[0].NewRow();
            oDr["FILE_ID"] = "a" + i;
            oDr["ID"] = "b" + i;
            oDs.Tables[0].Rows.Add(oDr);
        }
        for (int i = 1; i < 3; i++)
        {
            DataRow oDr = oDs.Tables[0].NewRow();
            oDr["FILE_ID"] = "c" + i;
            oDr["ID"] = "d" + i;
            oDs.Tables[0].Rows.Add(oDr);
        }
        oDs.AcceptChanges();

        DataTable odt1 = new DataTable();

        odt1.Columns.Add(new DataColumn("FILE_ID", typeof(string)));
        odt1.Columns.Add(new DataColumn("ID", typeof(string)));
        oDs.Tables.Add(odt1);
        oDs.AcceptChanges();

        for (int i = 1; i < 3; i++)
        {
            DataRow oDr = oDs.Tables[1].NewRow();
            oDr["FILE_ID"] = "a" + i;
            oDr["ID"] = "b" + i;
            oDs.Tables[1].Rows.Add(oDr);
        }
        for (int i = 1; i < 3; i++)
        {
            DataRow oDr = oDs.Tables[1].NewRow();
            oDr["FILE_ID"] = "c" + i;
            oDr["ID"] = "d" + i;
            oDs.Tables[1].Rows.Add(oDr);
        }
        oDs.AcceptChanges();

I need a LINQ query by which I can find if the combination of the values of rows (FILE_ID+ID) are unique & if they are the same in both the datatables

leppie
  • 115,091
  • 17
  • 196
  • 297
shiju87
  • 112
  • 2
  • 11
  • You have 2 tables. Each has multiple rows. So for a given table, you want to concatonate the row values (FILE_ID+ID) and compare that against the other rows of that table to determine if all results are unique? furthermore, you want to ensure both tables have the same content? – justin arsine Feb 28 '13 at 16:32
  • I don't want to concatenate the two rows but the combination of the values in a row must be unique and to ensure that both the table has the same row combination – shiju87 Feb 28 '13 at 16:38

1 Answers1

0

This gets you the non-unique values:

var notUnique = odt.AsEnumerable()
                            .GroupBy(x => (string) x["FILE_ID"] + x["ID"])
                            .Where(g => g.Count() > 1);

Finding the values that are in one table but not the other is found here Compares

Your particular case would look like this:

  var differentRows =
            odt.AsEnumerable().Where(
                o =>
                odt1.AsEnumerable().All(
                    o1 => ((string) o["FILE_ID"] + o["ID"]) != ((string) o1["FILE_ID"] + o1["ID"])))
                .Union(odt1.AsEnumerable().Where(
                o1 => odt.AsEnumerable().All(o => ((string)o["FILE_ID"] + o["ID"]) != ((string)o1["FILE_ID"] + o1["ID"]))));

Keep in mind this is like the "except" method where duplicate records wont present themselves as a difference. But since you are checking for dupes above I will assume no further checking is req'd.

Community
  • 1
  • 1
justin arsine
  • 133
  • 1
  • 7
  • This query was right until there were no DBNull values in any columns. If by accidently any DBNULL.Value came in any DataTable there would be problems. – shiju87 Mar 01 '13 at 04:55