开发者

Common and New rows in Data Table

开发者 https://www.devze.com 2023-03-23 22:28 出处:网络
I have two dataTables as follows 1)dtExistingZipCodeInDB (data fro开发者_运维问答m database) 2)dtCSVExcelSource (data from csv source which is to be processed)

I have two dataTables as follows

1) dtExistingZipCodeInDB (data fro开发者_运维问答m database)

2) dtCSVExcelSource (data from csv source which is to be processed)

I have two requirements

1) List all the retired zip codes (zip codes that are present in dtExistingZipCodeInDB but not in dtCSVExcelSource)

2) UnChanged zip codes (zip codes that are present both in dtExistingZipCodeInDB and dtCSVExcelSource)

I can use the Merge to get the retired zip codes. How do I get the unchanged zip codes?

Framework: .Net 3.0

//Note: dtExistingZipCodeInDB and dtCSVExcelSource has the same columns
dtCSVExcelSource.Merge(dtExistingZipCodeInDB);
DataTable dtRetiredZipDataTable = dtCSVExcelSource.GetChanges();
string retiredZipCodes = GetStringFromDataTable(dtRetiredZipDataTable, "ZipCode");

Thanks

Lijo


With the .NET 3.0 requirement, the Intersect LINQ extension method is not available, but you can provide your own extension method.

All you need is the MatchingRows extension method (see below in the demo code) and then do:

 IEnumerable<DataRow> unchangedZipCodes = dtExistingZipCodeInDB.MatchingRows(dtCSVExcelSource, "ZipCode");

Then you can loop over unchangedZipCodes, which will contain only those rows with ZipCodes in common between dtExistingZipCodeInDB and dtCSVExcelSource.

Below is demo code I wrote using LINQPad. I love LINQPad -- it's great for proof of concept or scratchpadding/sandboxing some code quickly. But it is not required for the solution to this question.

void Main()
{
    string colname = "ZipCode";

    var dt = new DataTable();
    dt.Columns.Add(colname, typeof(string));
    dt.Rows.Add(new [] { "12345" } );
    dt.Rows.Add(new [] { "67890" } );
    dt.Rows.Add(new [] { "40291" } );

    var dt2 = new DataTable();
    dt2.Columns.Add(colname, typeof(string));
    dt2.Rows.Add(new [] { "12345" } );
    dt2.Rows.Add(new [] { "83791" } );
    dt2.Rows.Add(new [] { "24520" } );
    dt2.Rows.Add(new [] { "48023" } );
    dt2.Rows.Add(new [] { "67890" } );

/// With .NET 3.5 LINQ extensions, it can be done inline.
//  var results = dt.AsEnumerable()
//          .Select(r => r.Field<string>(colname))
//          .Intersect(dt2.AsEnumerable()
//              .Select(r => r.Field<string>(colname)));
//  Console.Write(String.Join(", ", results.ToArray()));

    var results = dt.MatchingRows(dt2, colname);
    foreach (DataRow r in results)
        Console.WriteLine(r[colname]);
}

public static class Extensions
{
    /// With .NET 3.0 and no LINQ, create an extension method using yield.
    public static IEnumerable<DataRow> MatchingRows(this DataTable dt, DataTable dtCompare, string colName)
    {
        foreach (DataRow r in dt.Rows)
        {
            if (dtCompare.Select(String.Format("{0} = {1}", colName, r[(colName)])).Length > 0)
                yield return r;
        }
    }
}

Outputs:

    12345
    67890
0

精彩评论

暂无评论...
验证码 换一张
取 消