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

推荐订阅源

C
Cisco Blogs
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Martin Fowler
Martin Fowler
T
Tor Project blog
N
Netflix TechBlog - Medium
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
V
Visual Studio Blog
GbyAI
GbyAI
PCI Perspectives
PCI Perspectives
D
DataBreaches.Net
Jina AI
Jina AI
H
Heimdal Security Blog
云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
A
About on SuperTechFans
J
Java Code Geeks
美团技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News | PayPal Newsroom
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
博客园 - 司徒正美
C
Check Point Blog
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
Cisco Talos Blog
Cisco Talos Blog
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
D
Docker
博客园 - Franky
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - 王继坤

DataTable 递归 简单的程序,来实现无限级列表 结合 jquery.table.js 实现 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 sql 扩展方法 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 实现多字段综合查询
asp.net mvc 2 简简单单做开发 使用DataContext扩展方法Find<TEntity>(TEntity obj) 遇到的问题
王继坤 · 2010-07-06 · via 博客园 - 王继坤

  使用DataContext扩展方法Find<TEntity>(TEntity obj) 遇到一个问题,如果字段可以为空,提示错误信息:

没有为类型“System.Nullable`1[System.Int32]”和“System.Int32”定义二进制运算符 Equal。 
不知到是什么问题,如果字段不为空的话,查询一切正常。希望大家能帮助解决一下。

Find<TEntity>(TEntity obj)方法用途:通过定义实体类,查询符合它的所有记录。

代码

  Expression right = Expression.Constant(p.GetValue(obj, null));
                                    Expression left 
= Expression.Property(param, p.Name);
                                    Expression filter 
= Expression.Equal(left, right);

  主要问题在于 left.Type 是System.Nullable<System.Int32> ,而 right.Type 是System.Int32 。提示错误在:Expression filter = Expression.Equal(left, right);
Expression 的Type为只读。怎样能让两个类型统一,或者能采用其他的方法来避开这个问题。

执行过程:Aritle art=new Article();art.Status=1; var list=db.Find<Article>(art);

Find<TEntity>(TEntity obj) 代码:

 1   /// <summary>
 2         /// 实现查找
 3         /// </summary>
 4         /// <typeparam name="TEntity"></typeparam>
 5         /// <param name="obj"></param>
 6         /// <returns></returns>
 7         public IQueryable<TEntity> Find<TEntity>(TEntity obj) where TEntity : class
 8         {
 9             //获得所有property的信息
10             PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
11             //构造初始的query
12 
13             IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
14             //遍历每个property
15             foreach (PropertyInfo p in properties)
16             {
17                 if (p != null)
18                 {
19                     Type t = p.PropertyType;
20                     //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
21                     if (t.IsValueType || t == typeof(string|| t == typeof(System.Byte[])
22                       || t == typeof(object|| t == typeof(System.Xml.Linq.XDocument)
23                       || t == typeof(System.Data.Linq.Binary))
24                     {
25                         //如果不为null才算做条件
26 
27                         if (p.GetValue(obj, null!= null && p.GetValue(obj, null).ToString() != "0001/1/1 0:00:00" && p.GetValue(obj, null).ToString() != "0001-01-01 0:00:00" && p.GetValue(obj, null).ToString() != "0")
28                         {
29                             if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj, null)) == 0)
30                             {
31 
32                             }
33                             else
34                             {
35                                 ParameterExpression param = Expression.Parameter(typeof(TEntity), "c");
36                                
37                                 if (t != typeof(string))
38                                 {
39                                     Expression right = Expression.Constant(p.GetValue(obj, null));
40                                     Expression left = Expression.Property(param, p.Name);
41                                     Expression filter = Expression.Equal(left, right);
42                                     Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);
43                                   //  Expression SecondCondition = Expression.Call(
44                                   //Expression.Property(param, p.Name)
45                                   //, p.PropertyType.GetMethod("Equals")
46                                   //, Expression.Constant(p.GetValue(obj, null), typeof(string)));
47                                   //  Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(SecondCondition, param);
48                                     query = query.Where(pred);
49                                 }
50                                 else
51                                 {
52                                     Expression SecondCondition = Expression.Call(
53                                     Expression.Property(param, p.Name)
54                                     , typeof(string).GetMethod("Contains")
55                                     , Expression.Constant(p.GetValue(obj, null), typeof(string)));
56                                     Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(SecondCondition, param);
57                                     query = query.Where(pred);
58                                 }
59 
60                             }
61                         }
62                     }
63                 }
64             }
65             return query;
66         }