2016-08-13 110 views
0

在下面的代码,我需要明确提到CountryIdCountryName但我想,以避免和试图建立一个generic method转换一个通用的IEnumerable <T>到了IEnumerable <KeyValuePair>(C#)

public struct KeyValueStruct 
{ 
    public int Key { get; set; } 
    public string Value { get; set; } 
} 

private static IEnumerable<KeyValueStruct> ConvertPocoToKeyValueList(IEnumerable<CountryPoco> list) 
{ 
    var result = new List<KeyValueStruct>(); 

    if (list != null) 
    { 
     foreach (var item in list) 
     { 
      result.Add(new KeyValueStruct() 
      { 
       Key = item.CountryId, 
       Value = item.CountryName 
      }); 
     } 
    } 

    return result; 
} 

我从列表中知道第一个属性总是整数(在本例中是CountryId),第二个属性是String。

我想使用Generics来实现,但我不确定这是否是最好的方法,请参阅我的建议代码(虽然它不工作)。

private static IEnumerable<KeyValueStruct> ConvertPocoToKeyValueList<T>(T list) 
{ 
    var result = new List<KeyValueStruct>(); 

    if (list != null) 
    { 
     foreach (var item in list) 
     { 
      result.Add(new KeyValueStruct() 
      { 
       Key = item.CountryId, 
       Value = item.CountryName 
      }); 
     } 
    } 

    return result; 
} 

如果您有更好的主意来达到同样的效果,那么请提出建议。

+1

什么是KeyValueStruct?任何你不使用.NET框架中的KeyValuePair 的理由?请注意,LINQ使所有这些微不足道的,btw ...'var result = countries.Select(c => new KeyValuePair (c.CountryId,c.CountryName).ToList();'当然,你需要处理输入为空,也许......但是你可能会发现,防止这种情况发生会更好。 –

+0

@JonSkeet:我意识到我错过了KeyValueStruct,并且刚刚添加到问题中。具有自定义结构在性能方面要快得多,所以我决定自己实现,而不是使用.NET的默认KeyValuePair。有没有办法避免对'c.CountryId,c.CountryName'进行硬编码? –

+0

Um ,'KeyValuePair'已经是一个结构体......你认为你自己的'KeyValueStruct'类型的速度更快吗?无可否认这是一个可变值类型,这是一个不同的...但不是一个积极的类型 –

回答

2

您可以通过传递要用作键和值的属性来使该泛型。我认为使用名为KeyValuePair<Tkey, TValue>通用struct比重新发明轮子自己更好:

private static IEnumerable<KeyValuePair<Tkey, TValue>> 
         ConvertPocoToKeyValueList<TSource, Tkey, TValue> 
            (IEnumerable<TSource> list, 
            Func<TSource, Tkey> keySelector, 
            Func<TSource, TValue> valueSelector) 
     { 
      return list.Select(item => new KeyValuePair<Tkey, TValue> 
              (keySelector(item), valueSelector(item))); 
     } 

用法:

var result = ConvertPocoToKeyValueList(list, x=> x.CountryId, x=> x.CountryName); 

你甚至可以做,没有使用这种通用的方法,通过直接使用:

var result = list.Select(item => new KeyValuePair<Tkey, TValue> 
               (item.CountryId, item.CountryName)); 
+1

不需要'foreach',只需要使用Linq'Select',更加整洁。 – DavidG

+0

也不需要'TKey'和'TValue'类型,我们已经知道'KeyValueStruct'的结构 – DavidG

+0

@DavidG我真的觉得这个方法是没用的,因为用户可以使用Select来创建他想要的而不需要创建一个通用的方法只调用另一个泛型方法。 – user3185569

相关问题