LINQ Dynamic 动态查询遇到的问题
2009-09-17 10:31
281 查看
1 sb.Append(string.Format(" AND TFJ007.BTHDATE <= Convert.ToDateTime(\"{0}\") ",
2 DateTime.Now.AddYears(-50).ToShortDateString()));
3
4 if (chkPayment.Items[0].Selected)
5 sb.Append(string.Format(" AND TBL_CUSTOMER.PAYMENT = \'N\' "));
6 if (chkPayment.Items[1].Selected)
7 sb.Append(string.Format(" AND TBL_CUSTOMER.PAYMENT = \'M\' "));
8
使用Dynamic.cs 报错
The error thrown is:
No property or field 'BTHDATE' exists in type 'EntitySet`1'
后来查了下文档,得到以下结果:
Based on your description, I think you are using LINQ to SQL and dynamic LINQ library. As you have mentioned, the First extension method is not supported in dynamic LINQ. Because when searching methods of certain entity types, the dynamic LINQ library does not search the extension methods. That’s reasonable, because the extension methods can be defined outside the property’s original assembly, and it is quite inefficiency and impossible to search the all the possible .NET assemblies.
One workaround is to modify the implementation of the dynamic LINQ library to let First extension method be found. We can modify the method FindMethod in Dynamic.cs. If the method name is First, we find the certain extension method of the EntitySet. For detail, please see
==================================================================
int FindMethod(Type type, string methodName, bool staticAccess, Expression[] args, out MethodBase method)
{
if (methodName == "First")
{
// Load the extension method's assembly
Assembly assembly = Assembly.LoadFrom(@"C:"Program Files"Reference Assemblies"Microsoft"Framework"v3.5"System.Core.dll");
// Retrieve all the method named First
var query = from t in assembly.GetTypes()
where t.Name == "Queryable"
from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
where m.Name == "First"
select m;
// Get the method
method = query.First();
return 1;
}
BindingFlags flags = BindingFlags.Public | BindingFlags.DeclaredOnly |
(staticAccess ? BindingFlags.Static : BindingFlags.Instance);
foreach (Type t in SelfAndBaseTypes(type)) {
MemberInfo[] members = t.FindMembers(MemberTypes.Method,
flags, Type.FilterNameIgnoreCase, methodName);
int count = FindBestMethod(members.Cast<MethodBase>(), args, out method);
if (count != 0) return count;
}
method = null;
return 0;
}
==================================================================
Then we can use the First extension method on the EntitySet:
==================================================================
string searchCondition = "EASummaries.First().AssessmentCompletedSuccessfully == Convert.ToBoolean(""True"")";
var seesions = DAL.ExpertAssessments.DefaultIfEmpty().Where(searchCondition);
==================================================================
Besides, if we know the which EntitySet we want to query, directly calling the First extension method is easier.
==================================================================
string searchCondition = "AssessmentCompletedSuccessfully == Convert.ToBoolean(""True"")";
var seesions = DAL.ExpertAssessments.DefaultIfEmpty().Select(e => e.EASummaries.First()).Where(searchCondition);
==================================================================
原文地址:http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/e3678e1e-b3f2-4bc3-9d6c-718dd874371d
最后代码修改为
if (chkIncludeOlder.Checked)
sb.Append(string.Format(" AND TFJ007.First().BTHDATE <= Convert.ToDateTime(\"{0}\") ",
DateTime.Now.AddYears(-50).ToShortDateString()));
if (chkPayment.Items[0].Selected)
sb.Append(string.Format(" AND TBL_CUSTOMER.PAYMENT = \'N\' "));
if (chkPayment.Items[1].Selected)
sb.Append(string.Format(" AND TBL_CUSTOMER.PAYMENT = \'M\' ")); 运行正常。这个情况出现在1对多的关系中。
相关文章推荐
- 今天遇到一个问题,linq语句的写法,查询动态字段
- EntityFramework v1 .Net3.5 Sp1 动态查询过滤检索所遇到的问题
- ibatis中动态查询返回字段返回用resultClass="java.util.HashMap" 的问题
- 调用企业信息查询接口展示数据并对外提供接口遇到的问题
- 关于使用ant-design设计可动态添加FormItem的过程中遇到的问题记录。
- iBatis的dynamic动态查询问题
- mysql between and 遇到日期查询边界问题
- [转]LinqToSql的问题 多条件 动态变化查询条件怎样查询
- Activity动态添加Fragment时遇到的问题
- 使用UNION查询时,同时使用TOP 与ORDER BY遇到的问题
- MySQL 查询中遇到的若干问题
- 项目中遇到的一个ibatis动态查询
- js循环动态绑定带参数函数遇到的问题及解决方案[转]
- MSSQL2000的官方JDBC在应用动态代理机制遇到的异常问题解决
- 关于动态生成DropDownList遇到的问题
- 动态获取自定义控件坐标中遇到的问题
- Unity引用自己编译的动态dll遇到的问题
- ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题
- 项目中遇到的HQL查询问题
- 关于C#SqlParameter传参进行模糊查询遇到的问题!