linq如何填写字典对象

时间:2013-02-27 17:50:59

标签: c# linq

我之前有过这个,我已经删除了KVP,只是拥有了Dictionary属性。

之前:

public class GranuleAttributes
{
    public Dictionary<string, KVP> attributes { get; set; }
}
public class KVP
{
    public string ID { get; set; }
    public string Value { get; set; }
}

后:

public class GranuleAttributes
{
    public Dictionary<string, string> attributes { get; set; }
}

在我使用以下LINQ填充词典之前:

  attributes =
    (
        from attrRow in granuleGroup.First()
        select new KVP()
        {
            ID = Convert.ToString(attrRow["AttributeID"],
            Value = (string)attrRow["AttributeValue"]
        }
    ).ToDictionary(p => p.ID)

我现在如何填充我的属性对象?

更新

好的完全披露,这是代码:

var groups = reader.Cast<IDataRecord>()
    .GroupBy(dr => new { ID = (int)dr["id"] })
    .GroupBy(g => g.Key.ID);

response.attributes = (
    from granuleGroup in groups
    let granuleRow = granuleGroup.First().First()
    select new USpaceDataAttributes()
    {
        id = (int)granuleRow["id"],
        endDate = Convert.ToDateTime(granuleRow["endDate"]),
        startDate = Convert.ToDateTime(granuleRow["startDate"]),
        type = !DBNull.Value.Equals(granuleRow["type"]) ? (string)granuleRow["type"] : "",
        attributes =
        (
            from attrRow in granuleGroup.First()
            select new KVP()
            {
                ID = !DBNull.Value.Equals(attrRow["AttributeID"]) ? Convert.ToString(attrRow["AttributeID"]) : string.Empty,
                Value = !DBNull.Value.Equals(attrRow["AttributeValue"]) ? (string)attrRow["AttributeValue"] : string.Empty
            }
        ).ToSerializableDictionary(p => p.ID)
    }
).ToList();

然后我拥有自己的类public class SerializableDictionary<TKey, TVal> : Dictionary<TKey, TVal>, IXmlSerializable, ISerializable,使用以下扩展方法。

#region ExtensionMethod
public static class CollectionExtensions
{

    public static SerializableDictionary<TKey, T> ToSerializableDictionary<TKey, T>(this IEnumerable<T> seq, Func<T, TKey> keySelector)
    {
        var dict = new SerializableDictionary<TKey, T>();
        foreach (T item in seq)
        {
            TKey key = keySelector(item);
            dict.Add(key, item);
        }

        return dict;
    }
} 
#endregion

2 个答案:

答案 0 :(得分:3)

我认为您不需要选择匿名对象或KVP对象

attributes = granuleGroup.First()
                 .ToDictionary(r => Convert.ToString(r["AttributeID"]),
                               r => Convert.ToString(r["AttributeValue"]));

更新:您应该为您的扩展方法添加值选择器

public static class CollectionExtensions
{    
    public static SerializableDictionary<TKey, TValue> ToSerializableDictionary<T, TKey, TValue>(
       this IEnumerable<T> source, Func<T, TKey> keySelector, Func<T, TValue> valueSelector)
    {
        var dict = new SerializableDictionary<TKey, TValue>();
        foreach (T item in source)
        {
            TKey key = keySelector(item);
            TValue value = valueSelector(item);
            dict.Add(key, value);
        }

        return dict;
    }
} 

答案 1 :(得分:1)

attributes =
    (
        from attrRow in granuleGroup.First()
        select new {            
            Key = Convert.ToString(attrRow["AttributeID"],
            Value = (string)attrRow["AttributeValue"]
        }
    ).ToDictionary(p => p.Key, p => p.Value)