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

推荐订阅源

有赞技术团队
有赞技术团队
G
Google Developers Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
P
Proofpoint News Feed
V
Visual Studio Blog
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 叶小钗
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
H
Help Net Security
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
量子位
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 王继坤

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&...
王继坤 · 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         }