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

推荐订阅源

L
LangChain Blog
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
博客园 - 叶小钗
博客园 - 【当耐特】
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cyberwarzone
Cyberwarzone
D
Darknet – Hacking Tools, Hacker News & Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google Online Security Blog
Google Online Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
N
News | PayPal Newsroom
IT之家
IT之家
GbyAI
GbyAI
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
GRAHAM CLULEY
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Visual Studio Blog
Help Net Security
Help Net Security
博客园 - Franky
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
酷 壳 – CoolShell
酷 壳 – CoolShell
Cloudbric
Cloudbric
Vercel News
Vercel News
Y
Y Combinator Blog
T
Troy Hunt's Blog
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
C
Cisco Blogs
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
V2EX - 技术
V2EX - 技术
V
Vulnerabilities – Threatpost
I
InfoQ
人人都是产品经理
人人都是产品经理
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
AI
AI
B
Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
F
Fortinet All Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 王继坤

DataTable 递归 简单的程序,来实现无限级列表 结合 jquery.table.js 实现 asp.net mvc 2 简简单单做开发 使用DataContext扩展方法Find<TEntity>(TEntity obj) 遇到的问题 asp.net mvc 2 简简单单做开发 实现基本数据操作操作RepositoryController<T> asp.net mvc 2 简简单单做开发 自定义Controller基类 - 王继坤 asp.net mvc 2 简简单单做开发 自定义DropdownList控件 asp.net mvc 2 简简单单做开发 通用增删改基本操作通用页面 asp.net mvc 2 DisplayTemplates 的使用 FcDigg 源码 asp.net 3.5 linq to access 简单实现 实例demo linq to access 简单实现 asp.net mvc 随想 asp.net mvc ajax实现1 asp.net ajax fccms 小型简单个人blog源码 FCKEDITOR中文使用说明 js调用 dotfuscator.exe 采用线程生成静态页面 新的开始--- gridview 实现多字段综合查询
linq to sql 扩展方法
王继坤 · 2009-06-22 · via 博客园 - 王继坤

      linq to sql 给我们带来很多惊奇的地方,减少我们的代码,节省了我们的时间,但是为了更方便做了一些小的扩展,部分内容来自网络。

      通过实体类插叙db.find<T>(T t):

  public IQueryable<TEntity> Find<TEntity>(TEntity obj) where TEntity : class
        {
            //获得所有property的信息
            PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            //构造初始的query
        
            IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
            //遍历每个property
            foreach (PropertyInfo p in properties)
            {
                if (p != null)
                {
                    Type t = p.PropertyType;
                    //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
                    if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
                      || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
                      || t == typeof(System.Data.Linq.Binary))
                    {
                        //如果不为null才算做条件

                        if (p.GetValue(obj, null) != null)
                        {
                            if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj, null)) == 0)
                            {

                            }
                            else
                            {

                              //构造linq 表达式
                                ParameterExpression param = Expression.Parameter(typeof(TEntity),"c");
                                Expression right = Expression.Constant(p.GetValue(obj, null));
                                Expression left = Expression.Property(param, p.Name);
                                Expression filter = Expression.Equal(left, right);
                                Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);
                                query = query.Where(pred);
                            }
                        }
                    }
                }
            }
            return query;
        }

      通过类的主键查询:

 public TEntity FindKey<TEntity>(object value) where TEntity : class
        {
            //获得所有property的信息
            PropertyInfo[] properties = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            //构造初始的query
            IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
            //遍历每个property
            foreach (PropertyInfo p in properties)
            {
                if (p != null)
                {
                    Type t = p.PropertyType;
                    //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
                    if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
                      || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
                      || t == typeof(System.Data.Linq.Binary))
                    {
                        //如果不为null才算做条件


                        if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)
                        {
                            ParameterExpression param = Expression.Parameter(typeof(TEntity), "d");
                            Expression right = Expression.Constant(value);
                            Expression left = Expression.Property(param, p.Name);
                            Expression filter = Expression.Equal(left, right);

                            Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);

                            query = query.Where(pred);
                            break;

                        }


                    }
                }
            }
            return query.First();
        }

    }

      通过实体类更新:

public void Update<TEntity>(TEntity obj) where TEntity : class
        {
            string str = "update  [" + typeof(TEntity).Name + "] set ";
            string cols = "";
            string where="";
            //获得所有property的信息
            PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            //构造初始的query

            IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
            //遍历每个property
            foreach (PropertyInfo p in properties)
            {
                if (p != null)
                {
                    Type t = p.PropertyType;
                    //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
                    if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
                      || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
                      || t == typeof(System.Data.Linq.Binary))
                    {
                        //如果不为null才算做条件

                        if (p.GetValue(obj, null) != null)
                        {
                            if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)
                            {
                                where +=" where ["+p.Name+"]="+p.GetValue(obj,null);
                            }
                            else
                            {

                                if (!(t.ToString().ToLower().Contains("string") || t.ToString().ToLower().Contains("datetime")))
                                    cols += "["+p.Name + "]=" + p.GetValue(obj, null) + ",";
                                else
                                    cols += "["+p.Name + "]='" + p.GetValue(obj, null) + "',";
                            }
                        }
                    }
                }
            }

            str += cols.Substring(0,cols.Length-1) +where;
             HttpContext.Current.Response.Write("<br>"+str+"<br>");
             this.ExecuteCommand(str);
         
        }
        public void UpdateAll<TEntity>(IEnumerable<TEntity> obj) where TEntity : class
        {
            foreach (var item in obj)
            {
                this.Update<TEntity>(item);
            }
        }

  更新、删除、添加代码类似,也可以实现多条数据的数据操作。代码和更新类似。代码下载地址http://fccms.googlecode.com/files/linqtoaccess.rar 里面得db.cs文件里。