常用Linq示例代码

   class Program
    {
        static void Main(string[] args)
        {
 
            //1. Aggregate
            int[] testArr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            //refer:http://www.cnblogs.com/ldp615/archive/2009/08/12/1544688.html
            // x is aggregate result 
            var result = Enumerable.Aggregate(testArr, (x, y) => { return x + y; });
            var result2 = Enumerable.Aggregate(testArr, calculateSum);
 
            string[] testStringArr = new string[] { "i", "am", "chinese" };
            var resultString = testStringArr.Aggregate((x, y) => { return x + "  " + y; });
 
            //2. All
            var allOK = testArr.All(o => o % 2 == 0);
            var allOK2 = testArr.All(o => o < 10);
 
            //3.Cast
            var testArr2 = Enumerable.Repeat(new CD() { Title = "a" }, 10);
            var ret = testArr2.Cast<ABC>().First();
 
            //4.group by
            var groupArr = testArr.GroupBy(o => o % 2 == 0);
            var allKeys = groupArr.Select(o => o.Key);
            var allKeysWithValues = groupArr.SelectMany(o => o.Select(x => x + " " + o.Key)).ToList();
 
            //5.Intersect
            int[] testArr3 = new int[] { 1, 2, 3, 9, 11, 12 };
            var intersectResult = testArr.Intersect(testArr3).ToList();
 
            //6.MAX, 
            var retMAX = testStringArr.Max(o => compareMAX(o));
 
 
            //7.single 
            //refer: http://blog.163.com/li_crane/blog/static/1950209720110853413858/
            //Single():操作一个集合,同时强要求只有一个对象匹配,并返回这一个。
            //First():操作一个集合,可以有多个对象匹配,但是只返回第一个。
            //Take(1):操作一个集合,可以有对个对象匹配,单只返回第一个,但是这里返回的是一个集合,而不是单个的概念。
 
            //8.Orderby ThenBy
            int i = 0;
            List<CD> allCDs = new List<CD>();
            while (i < 10)
            {
                allCDs.Add(new CD() { Title = i + "i", SubTitle = DateTime.Now.ToString(), TypeID = i % 3 });
                i++;
            }
            var newList = allCDs.OrderBy(o => o.SubTitle).ThenBy(o => o.Title);
 
            //9.ToDictionary
            var retDictionary = allCDs.ToDictionary(x => x.Title, y => y);
 
            //10.ToLookup
            //refer:http://blog.sina.com.cn/s/blog_5fc933730100xd3d.html
            var retLookup = allCDs.ToLookup(x => x.TypeID);
            var secondElement = retLookup[2];
 
            //1.Zip
            //refer:http://www.cnblogs.com/lifepoem/archive/2011/11/29/2267243.html
            IEnumerable<string> zip = testArr.Zip(allCDs, (n, w) => n + "=" + w.Title + "|" + w.SubTitle);
 
            foreach (var zipstr in zip)
            {
                Trace.WriteLine(zipstr);
            }
        }
 
        static int calculateSum(int a, int b)
        {
            return a + b;
        }
 
        static T compareMAX<T>(T parm)
        {
            return parm;
        }
    }
 
 
    class ABC
    {
        public string Title { get; set; }
    }
 
    class CD : ABC
    {
        public string SubTitle { get; set; }
        public int TypeID { get; set; }
    }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

上一篇:ManagementFactory (二) getMemoryMXBean


下一篇:hibernate结合使用gson转换json中一点看法