C#中linq

class IntroToLINQ
{
    static void Main()
    {
        // The Three Parts of a LINQ Query:
        //  1. Data source.
        int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

        // 2. Query creation.
        // numQuery is an IEnumerable<int>
        var numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;

        // 3. Query execution.
        foreach (int num in numQuery)
        {
            Console.Write("{0,1} ", num);
        }
    }
}
Northwnd db = new Northwnd(@"c:\northwnd.mdf");

// Query for customers in London.
IQueryable<Customer> custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

List<int> numQuery2 =
    (from num in numbers
     where (num % 2) == 0
     select num).ToList();

// or like this:
// numQuery3 is still an int[] 

var numQuery3 =
    (from num in numbers
     where (num % 2) == 0
     select num).ToArray();

var queryLondonCustomers = from cust in customers
                           where cust.City == "London"
                           select cust;
// queryCustomersByCity is an IEnumerable<IGrouping<string, Customer>>
  var queryCustomersByCity =
      from cust in customers
      group cust by cust.City;

  // customerGroup is an IGrouping<string, Customer>
  foreach (var customerGroup in queryCustomersByCity)
  {
      Console.WriteLine(customerGroup.Key);
      foreach (Customer customer in customerGroup)
      {
          Console.WriteLine("    {0}", customer.Name);
      }
  }
class Student
{
    public string First { get; set; }
    public string Last {get; set;}
    public int ID { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public List<int> Scores;
}

class Teacher
{
    public string First { get; set; }
    public string Last { get; set; }
    public int ID { get; set; }
    public string City { get; set; }
}
class DataTransformations
{
    static void Main()
    {
        // Create the first data source.
        List<Student> students = new List<Student>()
        {
            new Student {First="Svetlana",
                Last="Omelchenko",
                ID=111,
                Street="123 Main Street",
                City="Seattle",
                Scores= new List<int> {97, 92, 81, 60}},
            new Student {First="Claire",
                Last="O’Donnell",
                ID=112,
                Street="124 Main Street",
                City="Redmond",
                Scores= new List<int> {75, 84, 91, 39}},
            new Student {First="Sven",
                Last="Mortensen",
                ID=113,
                Street="125 Main Street",
                City="Lake City",
                Scores= new List<int> {88, 94, 65, 91}},
        };

        // Create the second data source.
        List<Teacher> teachers = new List<Teacher>()
        {
            new Teacher {First="Ann", Last="Beebe", ID=945, City = "Seattle"},
            new Teacher {First="Alex", Last="Robinson", ID=956, City = "Redmond"},
            new Teacher {First="Michiyo", Last="Sato", ID=972, City = "Tacoma"}
        };

        // Create the query.
        var peopleInSeattle = (from student in students
                    where student.City == "Seattle"
                    select student.Last)
                    .Concat(from teacher in teachers
                            where teacher.City == "Seattle"
                            select teacher.Last);

        Console.WriteLine("The following students and teachers live in Seattle:");
        // Execute the query.
        foreach (var person in peopleInSeattle)
        {
            Console.WriteLine(person);
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
    The following students and teachers live in Seattle:
    Omelchenko
    Beebe
 */
var query = from cust in Customers
            select cust.City;
var query = from cust in Customer
            select new {Name = cust.Name, City = cust.City};
class XMLTransform
{
    static void Main()
    {
        // Create the data source by using a collection initializer.
        // The Student class was defined previously in this topic.
        List<Student> students = new List<Student>()
        {
            new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores = new List<int>{97, 92, 81, 60}},
            new Student {First="Claire", Last="O’Donnell", ID=112, Scores = new List<int>{75, 84, 91, 39}},
            new Student {First="Sven", Last="Mortensen", ID=113, Scores = new List<int>{88, 94, 65, 91}},
        };

        // Create the query.
        var studentsToXML = new XElement("Root",
            from student in students
            let x = String.Format("{0},{1},{2},{3}", student.Scores[0],
                    student.Scores[1], student.Scores[2], student.Scores[3])
            select new XElement("student",
                       new XElement("First", student.First),
                       new XElement("Last", student.Last),
                       new XElement("Scores", x)
                    ) // end "student"
                ); // end "Root" 

        // Execute the query.
        Console.WriteLine(studentsToXML);

        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
< Root>
  <student>
    <First>Svetlana</First>
    <Last>Omelchenko</Last>
    <Scores>97,92,81,60</Scores>
  </student>
  <student>
    <First>Claire</First>
    <Last>O‘Donnell</Last>
    <Scores>75,84,91,39</Scores>
  </student>
  <student>
    <First>Sven</First>
    <Last>Mortensen</Last>
    <Scores>88,94,65,91</Scores>
  </student>
</Root>
class FormatQuery
{
    static void Main()
    {
        // Data source.
        double[] radii = { 1, 2, 3 };

        // Query.
        IEnumerable<string> query =
            from rad in radii
            select String.Format("Area = {0}", (rad * rad) * 3.14);

        // Query execution.
        foreach (string s in query)
            Console.WriteLine(s);

        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
    Area = 3.14
    Area = 12.56
    Area = 28.26
*/

http://msdn.microsoft.com/en-us/library/bb397924.aspx

地址;http://msdn.microsoft.com/en-us/library/bb397927.aspx

C#中linq,布布扣,bubuko.com

时间: 2024-08-04 02:30:37

C#中linq的相关文章

Webform中linq to sql多条件查询(小练习)

多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件... aspx代码: 1 <body> 2 <form id="form1" runat="server"> 3 4 <br /> 5 <asp:Label ID="Label1" runat="server" Text="关键字:"></asp:Label&g

nodeJs中linq.js学习

一.在nodeJs 中package.json文件中添加linq模块 如:    "linq" : "3.0.5" 终端中执行npm install  linq模块会自动下载到node_modules中 sample/tutorial.js 是linq.js的demo  可以参考 二.使用 在javascript中使用linq与lambda 直接对Array和String进行扩展,可直接使用 同时扩展了Number.Date.Console的部分方法,方便使用 如

C#中LINQ的使用demo

1 public void showTolist(int type) 2 { 3 listbox.Items.Clear(); 4 listbox.Items.Add("姓名 年龄 手机号码"); 5 if (type == 1) 6 { 7 foreach (var n in list) 8 { 9 listbox.Items.Add(n.print()); 10 } 11 }else 12 if (type == 2) 13 { 14 var query1 = from t1 in

DataTable中Linq查询(where,group by)

//经理 List<string> jtlist = (from t in jtTable.AsEnumerable() group t by new { t1 = t.Field<string>("JTManager") } into m select new { JTManager = m.Key.t1 } into c where !string.IsNullOrEmpty(c.JTManager) select c.JTManager).ToList()

c# 中Linq Lambda 的ToLookup方法的使用

同样直接上代码: List<Student> ss = new List<Student>(); Student ss1 = new Student() { Id = 1, Age = 1, Name = "11" }; Student ss2 = new Student() { Id = 1, Age = 1, Name = "11" }; Student ss3 = new Student() { Id = 2, Age = 2, Nam

LinQ 基础

Linq to Sql:      LINQ TO SQL是包含在.NET Framework 3.5 版中的一种 O/RM 组件(对象关系映射),O/RM 允许你使用 .NET 的类来对关系数据库进行建模. 数据库访问技术包括:         (1)ADO.NET(基础)   (2)EF框架(集成)   (3)Linq(微软高集成) Linq:高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名 1.LinQ创建   新建-选择Linq to Sql-

LINQ Group By操作

假设我们需要从两张表中统计出热门商圈,这两张表内容如下: 上表是所有政区,商圈中的餐饮个数,名为FoodDistrict 下表是所有政区,商圈中的SPA个数,名为SPADistrict 现在要把这两张表,根据政区和商圈合并,然后相加Counts,根据Counts的总大小排序,统计热门商圈和热门政区. 在这里仅讨论合并的问题,以演示在SQLServer和C#中LINQ的实现方法: 通常,我们可以直接通过在SQLServer里面首先通过Union All,然后再通过GroupBy语句来执行查询操作即

转载:C#特性-表达式树

原文地址:http://www.cnblogs.com/tianfan/ 表达式树基础 刚接触LINQ的人往往觉得表达式树很不容易理解.通过这篇文章我希望大家看到它其实并不像想象中那么难.您只要有普通的LINQ知识便可以轻松理解本文. 表达式树提供一个将可执行代码转换成数据的方法.如果你要在执行代码之前修改或转换此代码,那么它是非常有价值的.尤其是当你要将C#代码----如LINQ查询表达式转换成其他代码在另一个程序----如SQL数据库里操作它. 但是我在这里颠倒顺序,在文章最后你很容易发现为

C#特性-表达式树

表达式树ExpressionTree 表达式树基础 转载需注明出处:http://www.cnblogs.com/tianfan/ 刚接触LINQ的人往往觉得表达式树很不容易理解.通过这篇文章我希望大家看到它其实并不像想象中那么难.您只要有普通的LINQ知识便可以轻松理解本文. 表达式树提供一个将可执行代码转换成数据的方法.如果你要在执行代码之前修改或转换此代码,那么它是非常有价值的.尤其是当你要将C#代码----如LINQ查询表达式转换成其他代码在另一个程序----如SQL数据库里操作它. 但