直接上代码,内容很浅显易懂,在这里就不做更多的解释,解释见代码注释。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading.Tasks; public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] names = { "Wang", "Biao", "WangBiao", "Kingtiger" };
var q1 = names.Where(a => a.StartsWith("W"));//直接查询
var q2 = from a in names
where a.StartsWith("W")
select a;//linq查询
var q3 = from a in names
where a.StartsWith("W")
orderby a descending
select a;//linq查询,包括排序,倒序要加上descending
var q4 = names.OrderBy(a => a).Where(a=>a.StartsWith("W"));//直接排序
var q5 = names.OrderByDescending(a => a).Where(a=>a.StartsWith("W"));//直接倒序排序 var q6 = from a in names
select new
{
HI = a + ",您好!"
};//查询后重构输出 var q7 = names.Select(a => new { HI = a + ",您好!" });//直接查询构造输出 List<Person> persons = new List<Person>();
persons.Add(new Person() { ID="0001",Name="Kingtiger" });
persons.Add(new Person() { ID = "0002", Name = "Wang" });
persons.Add(new Person() { ID = "0001", Name = "Biao" });
persons.Add(new Person() { ID = "0004", Name = "WangBiao" }); var q8 = from p in persons
orderby p.ID, p.Name
select p;//多级排序,先排序ID,然后再排序Name
var q9 = persons.OrderBy(a => a.ID).ThenBy(a => a.Name).Select(a => a);//直接两个排序 var qt = from p in persons
where p.Name.Contains("W") || p.Name.Contains("Biao")
select p;//第一次查询进行过滤
var q10 = from p in qt
orderby p.Name descending
select p.Name;//对一次查询结果进行第二次处理 var q11 = from p in persons
group p by p.ID into cg
select new {
GroupID = cg.Key,
GroupCount= cg.Count()
};//分组查询,将结果提取出来 List<Order> orders = new List<Order>();
orders.Add(new Order() { UserID="0002",OrderID="001", OrderName="苹果" });
orders.Add(new Order() { UserID = "0002", OrderID = "003", OrderName = "香蕉" });
orders.Add(new Order() { UserID = "0004", OrderID = "002", OrderName = "荔枝" }); var q12 = from p in persons
join o in orders on p.ID equals o.UserID
select new
{
PName = p.Name,
POrderName = o.OrderName
};
foreach (var p in q12)
{
Response.Write(p+"<br/>");
}
}
} public class Person
{
public string ID { set; get; }
public string Name { set; get; }
} public class Order
{
public string OrderID { set; get; }
public string OrderName { set; get; }
public string UserID { set; get; }
}

LinQ综合应用实例的更多相关文章

  1. 使用LINQ查询数据实例和理解

    使用LINQ查询数据实例和理解 var contacts= from customer in db.Customers where customer.Name.StartsWith("A&q ...

  2. 华为OSPF与ACL综合应用实例讲解

    OSPF与ACL综合应用实例讲解 项目案例要求: 1.企业内网运行OSPF路由协议,区域规划如图所示:2.财务和研发所在的区域不受其他区域链路不稳定性影响:3.R1.R2.R3只允许被IT登录管理:4 ...

  3. openresty 学习笔记小结:综合应用实例

    openresty 学习笔记小结:综合应用实例 这个综合实验实现的功能其实很简单,用户访问一个页面,显示一个默认页面.输入参数(post或者get都可以),如果参数在数据库查询得到并满足一定条件,根据 ...

  4. Directx 3D编程实例:多个3D球的综合Directx实例

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...

  5. [C#]Linq To Xml 实例操作- 转

    http://blog.sina.com.cn/s/blog_6c762bb301010oi5.html http://blog.xuite.net/cppbuilder/blog/9940157 在 ...

  6. 【C#】MVC+EF+LINQ 综合小项目

    第一,创建数据库 create table category(id int primary key,name nvarchar(20)) create table news(id int primar ...

  7. Linq 高级应用实例

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  8. Linq To Csv 实例简说

    http://www.codeproject.com/Articles/25133/LINQ-to-CSV-library 详细源代码在这里 https://github.com/mperdeck/L ...

  9. --@angularJS--较复杂的指令嵌套demo——综合小实例:登陆界面

    1.index.html: <!DOCTYPE HTML><html ng-app="app"><head>    <title>c ...

随机推荐

  1. Android journey 1@关于编码风格和命名规范

    /* * 1.关于编程风格:每一位程序猿可能都有自己独特的编程风格,但是有些规则是大家都必须遵守的,特别 * 是在工作的过程中,良好的代码风格能大大提高代码本身的可阅读性和维护性,也更有利于别人修改你 ...

  2. 工程移除CocoaPods依赖库

    http://zanderzhang.gitcafe.io/2015/09/26/工程移除CocoaPods依赖库/ 点这里--->CocoaPods安装和使用教程 当我们工程安装很多第三方开源 ...

  3. 关于windbg的认识

    1.windbg是一个用于调试代码的工具,基础介绍:http://www.pediy.com/kssd/pediy10/94457.html 2.关于windbg和vs在代码调试方面的区别,参考:ht ...

  4. WinForm中Component Class、User Control及Custom Control的区别和使用-转

    转http://www.cnblogs.com/jhtchina/archive/2010/11/28/1028591.html NET Framework 为您提供了开发和实现新控件的能力.除了常见 ...

  5. sqlserver 获取时间年月日时分秒

    转自:http://blog.itpub.net/14766526/viewspace-1156100/ select GETDATE() as '当前日期',DateName(year,GetDat ...

  6. Matlab中数组下标是logical,如何处理?

    K>> a = 10*ones(1,10); K>> b = [1 56 23 5 6 45 9 7 89 10]; K>> c = b<a c = 1 0 ...

  7. list 去掉重复的值

    去除List列表中重复值(3种解决方法)public static void main(String[] args) { String[] ar = { "dd", "c ...

  8. 使用shell脚本获取虚拟机中cpu使用率(读/proc/statc)

    #!/bin/bash interval= cpu_num=`-] -c` start_idle=() start_total=() cpu_rate=() cpu_rate_file=./`host ...

  9. What are Scopes?

    scope is an object that refers to the application model. It is an execution context for expressions. ...

  10. 安装ubuntu vi编辑无法正常使用的时候 如方向键变成ABCD

    http://blog.sina.com.cn/s/blog_7e3f6e8f0100vkon.html 在使用ubuntu的时候,发现vi编辑模式下退格键backspace和上下左右光标移动键不能用 ...