zoukankan      html  css  js  c++  java
  • LINQ Dynamic 动态查询遇到的问题

    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对多的关系中。

  • 相关阅读:
    不要试图给Password类型的TextBox赋值!
    asp.net与javacript之间的通讯
    这个iframe有点奇怪
    C#的异常处理机制 (转载)
    公匙算法.电子签名
    西煞魄工厂的入门教程
    Web services and SOAP
    观察者模式
    WebService:使用 Soap 标头自定义身份验证和授权(转载)
    社会型网络(3)-回到现实(转载)
  • 原文地址:https://www.cnblogs.com/yuanhuaming/p/1568301.html
Copyright © 2011-2022 走看看