• Linq的简单应用_01


        class Program
        {
            static void Main(string[] args)
            {
                //Linq to Sql
                DataContext dc = new DataContext(new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=12345"));
                var ttt =from b in dc.GetTable<GoodsTypes>() group b by b.UpId into D11 select new {
                    id = D11.Key,
                    data = D11
                };
                foreach (var item in ttt)
                {
                    Console.WriteLine("根据:"+item.id+"分组");
                    foreach (var item1 in item.data)
                    {
                        Console.WriteLine("	"+item1.TypeName);
                    }
                }
                var resutldc = from b in dc.GetTable<GoodsTypes>() select b;
                foreach (var item in resutldc)
                {
                    Console.WriteLine(item.TypeName);
                }
                dc.Dispose();
                //Linq to Xml
                Book[] books = {
                                   new Book{Name="小白",Year=2006,Title="汇编语言"}, 
                                   new Book{Name="小菜",Year=2006,Title="OOD"}, 
                                   new Book{Name="小静",Year=2002,Title="你不知道的C#"}, 
                               
                               };
                //需求:查询2006年的书籍,并生成Xml
                var d = new XElement("Books",
                    from b in books
                    where b.Year == 2006
                    select new XElement("book",
                        new XAttribute("Title", b.Title),
                        new XElement("year", b.Year)));
                Console.WriteLine(d);
                //Linq To Objects
                string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };
    
                var result = from b in words
                             orderby
                                 b ascending
                             group b by b.Length into groups
                             orderby groups.Key descending
                             select new
                             {
                                 lenth = groups.Key,
                                 data = groups
                             };
                foreach (var item in result)
                {
                    Console.WriteLine("world length:" + item.lenth);
                    foreach (var item1 in item.data)
                    {
                        Console.WriteLine("	" + item1);
                    }
                }
    
                Console.Read();
            }
    
        }
        [Table(Name = "GoodsTypes")]
        class GoodsTypes
        {
            [Column(IsPrimaryKey=true)]
            public int TypeId { get; set; }
            [Column(Name = "TypeName")]
            public string TypeName { get; set; }
            [Column]
            public string Memo { get; set; }
            [Column]
            public int UpId { get; set; }
        }
        class Book
        {
            public string Name { get; set; }
            public string Title { get; set; }
            public int Year { get; set; }
        }

    Hold on, everything is possible.
  • 相关阅读:
    典型用户及场景分析
    使用搜狗输入法个人感受
    第二期站立会议10
    寻找“水王”
    第二期站立会议9
    第二期站立会议8
    第二期站立会议7
    第二期站立会议6
    第二期站立会议5
    第二期站立会议4
  • 原文地址:https://www.cnblogs.com/student-note/p/6515174.html
Copyright © 2020-2023  润新知