当前位置:编程学习 > ASP.NET> 正文

linq 数据类型转换

时间:2015-10-25类别:编程学习

linq 数据类型转换

linq 数据类型转换

一、LINQ查询能够创建新类型,这一功能在select子句中实现。例如,可执行以下任务

 

1、将多个输入序列合并到具有新类型的单个输出序列中。

2、创建其元素只包含源序列中的各个元素的一个或几个属性的输出序列。

3、创建其元素包含对源数据执行的操作结果的输出序列。

4、创建不同格式的输出序列。

5、将多个输入联接到一个输出序列

 

二、linq 数据类型转换实例

 

  • 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml.Linq;
    
    namespace WebApplication1
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                List<Student> students = new List<Student>()
                {
                    new Student {First="",
                        Last="",
                        ID=111,
                        Street="复兴大道338",
                        City="杭州",
                        Scores=new List<int>{84,97,68,92}},      
                    new Student {First="",
                        Last="",
                        ID=111,
                        Street="复兴大道338",
                        City="云南",
                        Scores=new List<int>{84,97,68,92}},
                    new Student {First="",
                        Last="",
                        ID=111,
                        Street="复兴大道338",
                        City="芜湖",
                        Scores=new List<int>{84,97,68,92}},
                };
    
                List<Teacher> teachers = new List<Teacher>()
                {
                    new Teacher {First="",
                        Last="",
                        ID=111,
                        City="杭州"},   
                    new Teacher {First="",
                        Last="",
                        ID=111,
                        City="云南"},
                    new Teacher {First="",
                        Last="",
                        ID=111,
                        City="芜湖"}
                };
    
                //创建查询
                var peopleInSeattle = (from student in students
                                       where student.City == "芜湖"
                                       select student.First + student.Last)
                                         .Concat(from teacher in teachers
                                                 where teacher.City == "芜湖"
                                                 select teacher.First + teacher.Last);
                Response.Write("在芜湖的教师和学生:");
                //执行查询
                foreach (var person in peopleInSeattle)
                {
                    Response.Write(" " + person);
                }
            }
    
            class Student//学生类
            {
                public string First { set; get; }
                public string Last { set; get; }
                public int ID { set; get; }
                public string Street { set; get; }
                public string City { set; get; }
                public List<int> Scores;
            }
    
            class Teacher//教师类
            {
                public string First { set; get; }
                public string Last { set; get; }
                public int ID { set; get; }
                public string City { set; get; }
            }
    
        }
    }
    
    		
  •  

    三、使用linq将数据转换为XML格式

  •  
  • 
    List<Student> _list = new List<Student>() 
    { 
    new Student {FirstName="Svetlana", LastName="Omelchenko", 
                 Unid=111, Scores = new List<int>{97, 92, 81, 60}},
    
    new Student {FirstName="Claire", LastName="O’Donnell", 
                 Unid=112, Scores = new List<int>{75, 84, 91, 39}},
    
    new Student {FirstName="Sven", LastName="Mortensen", 
    标签:
  • 上一篇下一篇

    猜您喜欢

    热门推荐