eEhdsa

lambda's show time...

Case1:
写一个方法,输入一个表示整型的字符串列表,
并返回一个列表,包含其中偶数的平方,
并且需要按照平方后的结果排序”。
 1static List<int> GetSquaresOfPositive(List<string> strList)
 2{
 3    List<int> intList = new List<int>();
 4    foreach (var s in strList) intList.Add(Int32.Parse(s));
 5
 6    List<int> evenList = new List<int>();
 7    foreach (int i in intList)
 8    {
 9        if (i % 2 == 0) evenList.Add(i);
10    }

11
12    List<int> squareList = new List<int>();
13    foreach (int i in evenList) squareList.Add(i * i);
14
15    squareList.Sort();
16    return squareList;
17}

18
VS
 1static List<int> GetSquaresOfPositiveByLambda(List<string> strList)
 2{
 3    return strList
 4        .Select(s => Int32.Parse(s)) // 转成整数
 5        .Where(i => i % 2 == 0// 找出所有偶数
 6        .Select(i => i * i) // 算出每个数的平方
 7        .OrderBy(i => i) // 按照元素自身排序
 8        .ToList(); // 构造一个List
 9}

10
Where ==>  filter
Select  ==>  change


Case2:
列出所有的关键字,
根据其首字母进行分组,
并且要求对每组内部的关键字进行排序
static Dictionary<char, List<string>> GetIndex(IEnumerable<string> keywords)
{
    
// 定义字典
    var result = new Dictionary<char, List<string>>();

    
// 填充字典
    foreach (var kw in keywords)
    
{
        var firstChar 
= kw[0];
        List
<string> groupKeywords;

        
if (!result.TryGetValue(firstChar, out groupKeywords))
        
{
            groupKeywords 
= new List<string>();
            result.Add(firstChar, groupKeywords);
        }


        groupKeywords.Add(kw);
    }


    
// 为每个分组排序
    foreach (var groupKeywords in result.Values)
    
{
        groupKeywords.Sort();
    }


    
return result;
}

VS
1static Dictionary<char, List<string>> GetIndexByLambda(IEnumerable<string> keywords)
2{
3    return keywords
4        .GroupBy(k => k[0]) // 按照首字母分组
5        .ToDictionary( // 构造字典
6            g => g.Key, // 以每组的Key作为键
7            g => g.OrderBy(k => k).ToList()); // 对每组排序并生成列表
8}

9
Key & Count are property and function after group:
 1    // Summary:
 2    //     Represents a collection of objects that have a common key.
 3    //
 4    // Type parameters:
 5    //   TKey:
 6    //     The type of the key of the System.Linq.IGrouping<TKey,TElement>.This type
 7    //     parameter is covariant, as indicated by the out keyword. That is, after you
 8    //     specify T, you can use either the type you specified or any type that is
 9    //     more derived (that is, any class derived from T).This type parameter is covariant.
10    //     That is, you can use either the type you specified or any type that is more
11    //     derived. For more information about covariance and contravariance, see Covariance
12    //     and Contravariance in the Common Language Runtime.
13    //
14    //   TElement:
15    //     The type of the values in the System.Linq.IGrouping<TKey,TElement>.This type
16    //     parameter is covariant as described in the description for TKey.
17    public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable
18    {
19        // Summary:
20        //     Gets the key of the System.Linq.IGrouping<TKey,TElement>.
21        //
22        // Returns:
23        //     The key of the System.Linq.IGrouping<TKey,TElement>.
24        TKey Key get; }
25    }
 
These samples are collected from JeffreyZhao's blog, learn more from here

posted on 2009-09-02 10:13  eEhdsa  阅读(214)  评论(0编辑  收藏  举报

导航