zoukankan      html  css  js  c++  java
  • linq里lambda写的join查询,并附加动态拼接的条件,条件为enum类型的查询

    因为查询条件不固定的原因,sql式的linq查询没法动态拼接条件。

    网上搜的资料整理之后终于解决。

    参考资料:

    enum使用 http://blog.csdn.net/slowlifes/article/details/7799444

    linq动态查询 http://msdn.microsoft.com/zh-cn/subscriptions/dd470085.aspx

    lambda join使用 http://blog.csdn.net/lai_gb/article/details/4491843

           string whereParams = GetWhereParams(SolutionGroup, CapabilityType);
    
                var query = crm.new_usercapabilitynew_usercapabilities.Where(uc => uc.new_user == SystemUserId).
                    Join(
                        crm.new_capabilitynew_capabilities,
                        uc => uc.new_capabilityid,
                        c => c.new_capabilityid,
                        (uc, c) => new
                            {
                                uc,
                                c
                            }
                    ).Where(o => o.c.new_name.Contains(Capability)).Where(whereParams)
                    .OrderBy(o => o.uc.new_capabilityidLabel)
                    .Select(o => new
                    {
                        new_capabilityid = o.uc.new_capabilityid,
                        new_usercapabilityid = o.uc.new_usercapabilityid,
                        new_capabilityidLabel = o.uc.new_capabilityidLabel,
                        new_developmentarea = o.uc.new_developmentarea,
                        new_rating = o.uc.new_rating
                    });
         public string GetWhereParams(string SolutionGroup, string CapabilityType) 
            {
                var enumgroup = Enum.GetNames(typeof(Xrm.new_capability.NewSolutiongroup)).Where(e => e.ToLower().Contains(SolutionGroup.ToLower())).ToList();
                string groupLinqStr = "";
                if (enumgroup.Count > 0)
                {
                    groupLinqStr += "(";
                    for (int i = 0; i < enumgroup.Count; i++)
                    {
                        Xrm.new_capability.NewSolutiongroup enumitem = (Xrm.new_capability.NewSolutiongroup)Enum.Parse(typeof(Xrm.new_capability.NewSolutiongroup), enumgroup[i]);
                        groupLinqStr += "c.new_solutiongroup = " + ((int)enumitem).ToString();
                        if (i != enumgroup.Count - 1)
                        {
                            groupLinqStr += " or ";
                        }
                    }
                    groupLinqStr += ")";
                }
                else if (!string.IsNullOrEmpty(SolutionGroup))
                {
                    groupLinqStr += "c.new_solutiongroup = 0";
                }
    
                var enumtype = Enum.GetNames(typeof(Xrm.new_capability.NewCapabilitytype)).Where(e => e.ToLower().Contains(CapabilityType.ToLower())).ToList();
                if (enumtype.Count > 0)
                {
                    if (groupLinqStr.Length > 0) groupLinqStr += " and ";
                    groupLinqStr += "(";
                    for (int i = 0; i < enumtype.Count; i++)
                    {
                        Xrm.new_capability.NewCapabilitytype enumitem = (Xrm.new_capability.NewCapabilitytype)Enum.Parse(typeof(Xrm.new_capability.NewCapabilitytype), enumtype[i]);
                        groupLinqStr += "c.new_capabilitytype = " + ((int)enumitem).ToString();
                        if (i != enumtype.Count - 1)
                        {
                            groupLinqStr += " or ";
                        }
                    }
                    groupLinqStr += ")";
                }
                else if (!string.IsNullOrEmpty(CapabilityType))
                {
                    if (groupLinqStr.Length > 0) groupLinqStr += " and ";
                    groupLinqStr += "c.new_capabilitytype = 0";
                }
                return groupLinqStr;
            }
  • 相关阅读:
    使用Docker在本地搭建Hadoop分布式集群
    微博推荐 第三个map 源码
    对象
    http无状态(stateless)
    理解http的无连接
    http响应报文之首部行
    http响应报文之状态行
    http响应报文
    http请求报文之首部行
    http请求之请求数据
  • 原文地址:https://www.cnblogs.com/ariklee/p/3614263.html
Copyright © 2011-2022 走看看