LINQ to SQL Model Extension

[ 2008-02-25 13:50:14 | 作者: yuhen ]
字号: | |
对 LINQ to SQL 的扩展,增加了一些实用的方法。比如用 Lambda 表达式进行 Update / Delete 操作,而不是用 ExecuteCommand / ExecuteQuery 来执行一个 "老式" 的 SQL 字符串。使用 Lambda 表达式的好处很多 —— 重构, 代码完成, 数据库结构分离 等等。

下载本文后面的压缩包,添加到你的项目,当然还有 using namespace... 这些源代码对大家灵活运用 Lambda Expression 应该有些帮助。

使用演示
using Rainsoft.Framework.Extension;

using (var context = new PassportDataContext())
{
    // 更新字段
    context.Users.Update(u => u.UniqueId == "0", u => u.Score, 1000);

    // 更新多个字段
    //context.Users.Update(u => u.Username == "user1", u => new { u.Score, u.Enabled }, 1000, true); 

    // 递增(减)
    //context.Users.Increment(u => u.UniqueId == "0", u => u.Score, 10);
    //context.Users.Increment(u => u.UniqueId == "0", u => u.Score, -10);
    //context.Users.Increment(u => u.Status != null && (u.Score ?? 0) < 100, u => u.Score, -10);

    // 删除实体 (比先获取再删除要方便很多)
    //context.Users.Delete(u => u.Username == "user1"); 
    //context.Users.Delete(u => (u.Score ?? 0) == x); // 支持变量
    //context.Users.Delete(u => u.Status == null || ((u.Sex ?? 0) == 1 && (u.Score ?? 0) > 100));

    // 分页查询
    //var users = Context.Users.List(u => u.Id > 0, u => new { u.UniqueId, u.Username }, u => u.UniqueId, true, 
    //    3, 10, out count, out pageCount).ToArray(); 
    //
    // var topUsers = Context.Users.List(u => u.Id > 0, u => new { u.UniqueId, u.Username }, u => u.UniqueId, true, 
    //    5).ToArray();

    // 清空实体数据表
    //Context.Users.Truncate();
}

查询条件表达式(Predicate)支持复杂的多条件嵌套,包括对 NOT、LIKE、NOT LIKE、IN、NOT IN 等操作的支持,相关细节可参考源码中的注释。

点击下载
评论Feed 评论Feed: http://www.rainsts.net/feed.asp?q=comment&id=664

这篇日志没有评论。

发表评论
表情图标
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
UBB代码
转换链接
表情图标
悄悄话
用户名:   密码:  
验证码 * 请输入验证码