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

推荐订阅源

V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
V
V2EX
IT之家
IT之家
J
Java Code Geeks
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
D
Docker
S
Secure Thoughts
Recent Announcements
Recent Announcements
Webroot Blog
Webroot Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
博客园_首页
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Security Archives - TechRepublic
Security Archives - TechRepublic
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
I
InfoQ
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
W
WeLiveSecurity
T
Troy Hunt's Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Attack and Defense Labs
Attack and Defense Labs
美团技术团队
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
B
Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Scott Helme
Scott Helme
T
Tor Project blog
Know Your Adversary
Know Your Adversary
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
G
Google Developers Blog

博客园 - 王继坤

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文件里。