惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 叶小钗
F
Fortinet All Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 最新话题
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
PCI Perspectives
PCI Perspectives
Cloudbric
Cloudbric
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
IT之家
IT之家
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
M
MIT News - Artificial intelligence
Cisco Talos Blog
Cisco Talos Blog
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
G
Google Developers Blog
W
WeLiveSecurity
罗磊的独立博客
P
Privacy & Cybersecurity Law Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
V
Visual Studio Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Secure Thoughts
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
Blog — PlanetScale
Blog — PlanetScale
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed

博客园 - edobnet

phonegap 开发初探 .net C# ADC接口中DES加密算法 阿里软件接口开发基础(淘宝网) C# SQL 2005自动备份文件删除 短信猫与中国移动CMPP2。0收发短信实例 通过OSQL命令执行SQL SERVER批SQL 针对sql 2005优化的高性能分页存储过程 .net framework 2.0以上修改config配制更容易 - edobnet 远程注册表读取,与多线程池的应用. 使用JAVASCRIPT控制,IFAME的内容,从而使用投票点击等功能 ADSL自动断拨号类 类拟EXCEL表格锁定的功能的实现! - edobnet - 博客园 线程,线程数控制! sql Server 2000 分区视图的运用 利用.Net 线程池提高应用程序性能. google 中国编程大赛测试题,及自己的解答 触发器在增量同步数据的运用. 进程服务编写,与启动停止控制 js异步XMLhttpPost,并带有,等待显示,防正,XMLhttpPost请求时间过来,而使浏览器死掉! - edobnet - 博客园
LinQ操作汇总(From CSharpSamples)
edobnet · 2007-12-17 · via 博客园 - edobnet

基本方法可以查看这里http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx
最近学习LINQ资料不是很多,从CSHARPSAMPLE写一比较有用的例子方便大家学习。
1.INSERT/UPDATE/DELETE

insert Simple

insert one to Many

public void LinqToSqlInsert03() {

    Northwind db2 
= new Northwind(connString);

    DataLoadOptions ds 
= new DataLoadOptions();
    ds.LoadWith
<nwind.Employee>(p => p.EmployeeTerritories);
    ds.LoadWith
<nwind.EmployeeTerritory>(p => p.Territory);

    db2.LoadOptions 
= ds;
    var q 
= (
        from e 
in db.Employees
        where e.FirstName 
== "Nancy"
        select e);



    Console.WriteLine(
"*** BEFORE ***");
    ObjectDumper.Write(q, 
1);


    Console.WriteLine();
    Console.WriteLine(
"*** INSERT ***");
    var newEmployee 
= new Employee { FirstName = "Kira",
                                     LastName 
= "Smith"
                                   }
;
    var newTerritory 
= new Territory { TerritoryID = "12345",
                                       TerritoryDescription 
= "Anytown",
                                       Region 
= db.Regions.First()
                                     }
;
    var newEmployeeTerritory 
= new EmployeeTerritory { Employee = newEmployee,
                                                       Territory 
= newTerritory
                                                     }
;
    db.Employees.InsertOnSubmit(newEmployee);
    db.Territories.InsertOnSubmit(newTerritory);
    db.EmployeeTerritories.InsertOnSubmit(newEmployeeTerritory);
    db.SubmitChanges();


    Console.WriteLine();
    Console.WriteLine(
"*** AFTER ***");
    ObjectDumper.Write(q, 
2);



    Cleanup66();  
// Restore previous database state
}

update simple

update mutiple

public void LinqToSqlInsert06() {
    Console.WriteLine(
"*** BEFORE ***");
    ObjectDumper.Write(from c 
in db.OrderDetails where c.OrderID == 10255 select c);


    Console.WriteLine();
    Console.WriteLine(
"*** DELETE ***");
    
//Beverages
    OrderDetail orderDetail = db.OrderDetails.First(c => c.OrderID == 10255 && c.ProductID == 36);

    db.OrderDetails.DeleteOnSubmit(orderDetail);
    db.SubmitChanges();


    Console.WriteLine();
    Console.WriteLine(
"*** AFTER ***");
    ClearDBCache();
    ObjectDumper.Write(from c 
in db.OrderDetails where c.OrderID == 10255 select c);



    Cleanup69();  
// Restore previous database state
}

delete one to many

where

public void LinqToSqlWhere02() {
    var q 
=
        from e 
in db.Employees
        where e.HireDate 
>= new DateTime(199411)
        select e;

    ObjectDumper.Write(q);
}

first condition

public void LinqToSqlSelect01() {
    var q 
=
        from c 
in db.Customers
        select c.ContactName;

    ObjectDumper.Write(q);
}

public void LinqToSqlSelect04() {
    var q 
=
        from p 
in db.Products
        select 
new {p.ProductID, HalfPrice = p.UnitPrice / 2};
    ObjectDumper.Write(q, 
1);
}

public void LinqToSqlSelect05() {
    var q 
=
        from p 
in db.Products
        select 
new {p.ProductName, Availability = p.UnitsInStock - p.UnitsOnOrder < 0 ? "Out Of Stock""In Stock"};

    ObjectDumper.Write(q, 
1);
}

public void LinqToSqlSelect10() {
    var q 
= (
        from c 
in db.Customers
        select c.City )
        .Distinct();

    ObjectDumper.Write(q);
}

public void LinqToSqlCount01() {
    var q 
= db.Customers.Count();
    Console.WriteLine(q);
}

public void LinqToSqlCount02() {
    var q 
= db.Products.Count(p => !p.Discontinued);
    Console.WriteLine(q);
}

public void LinqToSqlCount03() {
    var q 
= db.Orders.Select(o => o.Freight).Sum();
    Console.WriteLine(q);
}

public void LinqToSqlCount05() {
    var q 
= db.Products.Select(p => p.UnitPrice).Min();
    Console.WriteLine(q);
}

public void LinqToSqlCount07() {
    var categories 
=
        from p 
in db.Products
        group p by p.CategoryID into g
        select 
new {
            CategoryID 
= g.Key,
            CheapestProducts 
=
                from p2 
in g
                where p2.UnitPrice 
== g.Min(p3 => p3.UnitPrice)
                select p2
        }
;

    ObjectDumper.Write(categories, 
1);
}

public void LinqToSqlCount08() {
    var q 
= db.Employees.Select(e => e.HireDate).Max();
    Console.WriteLine(q);
}

public void LinqToSqlCount11() {
    var q 
= db.Orders.Select(o => o.Freight).Average();
    Console.WriteLine(q);
}

public void LinqToSqlCount12() {
    var q 
= db.Products.Average(p => p.UnitPrice);
    Console.WriteLine(q);
}

public void LinqToSqlCount13() {
    var categories 
=
        from p 
in db.Products
        group p by p.CategoryID into g
        select 
new {
            g.Key, 
            ExpensiveProducts 
=
                from p2 
in g
                where p2.UnitPrice 
> g.Average(p3 => p3.UnitPrice)
                select p2
        }
;

    ObjectDumper.Write(categories, 
1);
}

join

public void LinqToSqlPaging01() {
    var q 
= (
        from c 
in db.Customers
        orderby c.ContactName
        select c)
        .Skip(
50)
        .Take(
10);

    ObjectDumper.Write(q);
}