lambda's show time...

本文通过两个案例展示了如何使用C#处理字符串列表:一是筛选并计算整数字符串列表中偶数的平方并排序;二是对关键字列表按首字母分组及排序。通过传统循环与LINQ方式对比,突出了LINQ简洁高效的特点。
Case1:
写一个方法,输入一个表示整型的字符串列表,
并返回一个列表,包含其中偶数的平方,
并且需要按照平方后的结果排序”。
 1 static  List < int >  GetSquaresOfPositive(List < string >  strList)
 2 ExpandedBlockStart.gifContractedBlock.gif {
 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)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 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
 1 static  List < int >  GetSquaresOfPositiveByLambda(List < string >  strList)
 2 ExpandedBlockStart.gifContractedBlock.gif {
 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)
ExpandedBlockStart.gifContractedBlock.gif
{
    
// 定义字典
    var result = new Dictionary<char, List<string>>();

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

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


        groupKeywords.Add(kw);
    }


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


    
return result;
}

VS
1 static  Dictionary < char , List < string >>  GetIndexByLambda(IEnumerable < string >  keywords)
2 ExpandedBlockStart.gifContractedBlock.gif {
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 ExpandedBlockStart.gifContractedBlock.gif     {
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>.
24ExpandedSubBlockStart.gifContractedSubBlock.gif        TKey Key get; }
25    }
 
These samples are collected from JeffreyZhao's blog, learn more from here

转载于:https://www.cnblogs.com/whoseyourlady/archive/2009/09/02/1558548.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值