Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # Lambda Expression

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use C # Lambda Expression, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

We handle some delete operations, choosing to write SQL statements or execute stored procedures, such as:

ItemDataContext db = new ItemDataContext (); db.ExecuteCommand ("DELETE FROM Item WHERE [CreateTime])

< {0}", DateTime.UtcNow.AddMonths(-1)); C# Lambda Expression的出现: 在程序里出现直接的SQL语句是一件很丑陋的事情。在我看来,数据库操作应该被封装起来,而对于应用层的开发人员来说,眼中应该只有对象--退一步的话也可向数据库发送指令(就是使用存储过程)。当然,这是理想状态,值得追求,但不可强求。幸运的是C# 3.0所拥有的强大特性足以让我们对LINQ to SQL的功能进行扩展。为了更好地进行项目开发,以及周五的一次技术交流,我为LINQ to SQL扩展了批量删除功能。当项目中引用了这个扩展之后,我们就可以使用如下的代码来实现上面的功能了: ItemDataContext db = new ItemDataContext(); db.Items.Delete(item =>

Item.CreateTime

< DateTime.UtcNow.AddMonths(-1)); 当然,扩展还支持更复杂的删除条件,例如: ItemDataContext db = new ItemDataContext(); db.Items.Delete(item =>

Item.CreateTime

< DateTime.UtcNow.AddMonths(-1) || item.ViewCount < item.CommentCount && item.UserName != "jeffz"); 之前我对于LINQ to SQL的扩展大都基于DataContext,不过很明显,这次的扩展是基于Table的。总的来说,这个扩展比我想象中要简单不少。针对LINQ的扩展最麻烦的地方就在于解析表达式树(Expression Tree),而这个扩展关键的就是二元表达式(BinaryExpression),除了这点就没有太大问题了--当然,这也是因为我放弃了对于复杂表达式树的解析,例如现在就不支持"item.Introduction.Length < 10"这种条件,而对于更完整的解析方式来说,应该将其转化为T-SQL中的LEN函数。 C# Lambda Expression的使用: 这个扩展的关键在于根据表达式树生成Where Condition,我使用三个步骤完成这个扩展,大家可以关注代码里的相关实现(如果需要的话我也可以在以后进行说明): ◆使用PartialEvaluator将表达式中的常量直接计算出来(例如"3 * 3"表达式将被替换为"9"),同时也会将一些存储在变量中的值使用常量进行替换。 ◆使用ConditionBuilder将表达式中的常量收集起来,并生成带参数的Condition表达式(例如"[CreateTime] < {0} AND [UserName] {1}")。 ◆使用DataContext.ExecuteCommand方法执行完整的SQL语句。 有了批量删除的功能,那么还缺点什么呢?那自然就是批量更新的功能了。批量更新的功能比删除略为复杂一些,我正在开发之中。在有了这个扩展之后,我们就可以使用如下的方法进行批量更新了: ItemDataContext db = new ItemDataContext(); db.Items.Update( item =>

New Item {Introduction = item.Title + "Hello World", ViewCount = item.ViewCount + 1,}, / / update mode item = > item.CommentCount > 100 / * update conditions * /); Thank you for reading this article carefully. I hope the article "how to use C # Lambda Expression" shared by the editor will be helpful to you. At the same time, I hope you will support me and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report