开发者

Joining a Dictionary to a Datatable

I have a dictionary (see below) that has Key strings and Value strings and I want to be able to join this dictionary to my datatable using the dictionary Key (The equivilant unique field in the Datatable is called Network_ID). How can I do this? I'm hoping for a new datatable that has the original datatable information as well as two additional columns of data (key and value from the dictionary). Thanks very much.

My dictionary is of this typ开发者_运维问答e: Dictionary<string, string> input = new Dictionary<string, string>();

Edited:

         dt.Columns.Add("Key");
         dt.Columns.Add("KeyValue");

        foreach (System.Data.DataRow row in dt.Rows)
        {
            var networkID = (string)row["Network_ID"];

            if(input.ContainsKey(networkID))
            {
                row["Key"] = networkID.ToString();
                row["KeyValue"] = input.Values.ToString();
            }

        }


It's fairly easy (with LINQ), though the result won't be a DataTable but an IEnumerable of a new anonymous class.

       var result = from myDataRow in myDataTable.AsEnumerable()
                    join myKeyValuePair in myDictionary
                    on myDataRow.Field("Network_ID") equals myKeyValuePair.Key
                    select new { 
                        NetworkID = myDataRow.Field("Network_ID"),
                        ... /* other DataTable row values here */,
                        DictionaryKey = myKeyValuePair.Key,
                        DictionaryValue = myKeyValuePair.Value
                    };

I hope that should get you going :) If you need a data table, you'll have to create a new one from the result of this LINQ query.


You can accomplish this by adding two columns to your data table, and looping through the rows to populate.

foreach (DataRow row in table.Rows)
{
    var networkID = (string)row["Network_ID"];
    if (input.ContainsKey(networkID))
    {
        row["NewKeyColumn"] = networkID;
        row["NewKeyValue"] = input[networkID]
    }
}

Because dictionary accesses are amortized O(1), the total join has linear performance.


As far as I know, assuming that you are using the DataTable class, you cannot join a Dictionary into your object.

You could create a new DataTable (within a DataSet) and transfer the contents of the Dictionary into the new Table, you could append new columns to the DataTable or you could, if it would by applicable to your situation, use a LINQ query and a join within the LINQ query to create an anonymous type (or you could create a new type that represents the combined data) and combine and use the data that way.

You should note that none of this will be able to make it's way back into the database if you require this information to be persisted. This would all be transient within your application.


something similar:

var result = dt.AsEnumerable()
                .Join(id2itm.Keys,
                        row => row[idScColIdx],
                        id => id,
                        (row, id) => row)
                .Each(ro => {ro[idClmName] = FlagChar;
                            ro[idColorClmName] = FlagColor;});

My Each() is an Extension method, you can use pure code:

var result = dt.AsEnumerable()
                .Join(id2itm.Keys,
                        row => row[idScColIdx],
                        id => id,
                        (row, id) => row);

foreach (var ro in result)
{
               ro[idClmName] = FlagChar;
               ro[idColorClmName] = FlagColor;});
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜