1  /  1  页   1 跳转 查看:1247

LINQ基本操作

LINQ基本操作

文/whhcode  出处/博客园

首先我们创建2个类,一个Category,一个Product


public class Product

    {

        public string Name { get; set; }

        public int CategoryID { get; set; }

    }

public class Category

    {

        public string Name { get; set; }

        public int ID { get; set; }

    }




  然后创建2个List的数据源     

static List<Category> categories = new List<Category>()

        {

            new Category(){Name="Beverages", ID=001},

            new Category(){Name="Condiments", ID=002},

            new Category(){Name="Vegetables", ID=003},

            new Category(){Name="Grains", ID=004},

            new Category(){Name="Fruit", ID=005},

            new Category(){Name="Other", ID=006}

        };



      static List<Product> products = new List<Product>()

      {

          new Product{Name="Cola",  CategoryID=001},

          new Product{Name="Tea",  CategoryID=001},

          new Product{Name="Mustard", CategoryID=002},

          new Product{Name="Pickles", CategoryID=002},

          new Product{Name="Carrots", CategoryID=003},

          new Product{Name="Bok Choy", CategoryID=003},

          new Product{Name="Peaches", CategoryID=005},

          new Product{Name="Melons", CategoryID=007},

        };


好了,现在我们开始使用LINQ了,下面使用的表达式和方法的结果是一样的,这里的方法指Lambda表达式

l  使用排序

1.        使用排序时使用的显示语句             

foreach (var c in orderByResult)

                Console.WriteLine("ID={0},Name={1}", c.ID, c.Name);


2.        升序

        表达式



var orderByResult = from c in categories
                            orderby c.ID //默认为升序,也可以在后面加上ascending
                            select c;
            方法

var orderByResult = categories.OrderBy(c=>c.ID);
3.    降序

    表达式   

var orderByResult = from c in categories
                    orderby c.ID descending
                    select c;

    方法 

var orderByResult = categories.OrderByDescending(c=>c.ID);

4.    多个属性的升序

    表达式

var orderByResult = from c in categories

                    orderby c.ID,c.Name

                    select c;


    方法

var orderByResult = categories.OrderBy(c=>c.ID).ThenBy(c=>c.Name);

5.    多个属性的降序排列

    表达式         

var orderByResult = from c in categories

                    orderby c.ID, c.Name descending

                    select c;


    方法

var orderByResult = categories.OrderByDescending(c=>c.ID).ThenByDescending(c=>c.Name);

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [Linq] 的这些文章也感兴趣:

Linq To Sql 项目从Beta迁移到RTM注意事项
Presentation: Erik Meijer on C# 3.0 and LINQ
LINQ基本操作
LINQ数据源ASP.NET项目入门
PLINQ改名为Parallel Extensions发布
.Net Compact Framework 3.5对Linq的支持
LINQ体验(16)——LINQ to SQL语句之DataContext
在Linq to Sql中使用记录的时间戳进行检测管理并发更新时的冲突
深入浅出学Linq-Linq to SQL DataContext的初始化
使用LinqExtender轻松实现自定义LINQ提供器
网络核战不可避免!
 

回复:LINQ基本操作

l  使用Where条件

1.    使用Where的显示语句

foreach (var w in whereResult)

      Console.WriteLine("ID={0},Name={1}", w.ID, w.Name);

2.    单个条件

          表达式


var whereResult = from c in categories

                          where c.Name == "Vegetables"

                          select c;


          方法

var whereResult = categories.Where(c => c.Name == "Vegetables");

3.    或条件(sql中的or)

    表达式

var whereResult = from c in categories

                  where c.Name == "Vegetables" || c.ID == 001

                  select c;


方法


var whereResult = categories.Where(c => (c.Name == "Vegetables" || c.ID == 001));
4.    与条件(sql中的and)

表达式   

var whereResult = from c in categories

                  where c.Name == "Vegetables" && c.ID == 001

                  select c;


方法

var whereResult = categories.Where(c => (c.Name == "Vegetables" && c.ID == 001));

l  连接

1.    内连接

表达式,注意在表达式中的使用join时相等要使用equals 

var innerJoinQuery = from c in categories

                    join prod in products on c.ID equals prod.CategoryID

                    orderby c.ID

                  select new { CategoryID = c.ID,CategoryName = c.Name, ProductName = prod.Name};


方法


var innerJoinQuery = categories.Join(products,//要连接的集合

            a=>a.ID ,//第一个集合的主键

            b =>b.CategoryID , //第二个集合中和第一个集合的主键关联的键,类型需要和第一个集合的主键相同

            (c,d) => //c对应于第一个集合中的对象,d对于与第二个集合中的对象,声明2个变量

            new {CategoryID = c.ID,CategoryName = c.Name,ProductName = d.Name} //最终返回的结果类型

                                      );

            foreach (var item in innerJoinQuery)

            {

                Console.WriteLine("CategoryID = {0},CategoryName={1},ProductName={2}",item.CategoryID, item.CategoryName, item.ProductName);

            }


2.    左连接

表达式


var leftJoinQuery = from c in categories

                    join prod in products on c.ID equals prod.CategoryID into cp

                  from t in cp.DefaultIfEmpty(new Product { Name = "Unknown", CategoryID = 006 })

                  select new { CategoryID = c.ID, CategoryName = c.Name, ProductName = t.Name };

            foreach (var item in leftJoinQuery)

            {

                Console.WriteLine("CategoryID = {0},CategoryName={1},ProductName={2}",item.CategoryID, item.CategoryName, item.ProductName);

            }


方法

l  分组

表达式


var groupResults = from c in categories

                          group c by c.ID;


    方法

var groupResults = categories.GroupBy(c => c.ID);
注意显示时的变化:

foreach (var item in groupResults)

            {

                Console.WriteLine(item.Key);

                foreach (var c in item)

                {

                    Console.WriteLine("    ID={0},Name={1}", c.ID, c.Name);

                }

        }

由于刚开始学习LINQ,对LINQ的理解并不深,难免有不足之处,请大家见谅!
网络核战不可避免!
 
1  /  1  页   1 跳转

快速回复帖子

标题
禁用 URL 识别
禁用表情
禁用 Discuz!NT 代码
使用个人签名
  [完成后可按 Ctrl+Enter 无刷新发布]  

版权所有 拼吾爱程序人生    Total Unique Visitors:

web counter

Powered by Discuz!NT 2.1.202   Copyright © 2001-2008 Comsenz Inc. 鄂ICP备07500843号
返顶部