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;
        }

 

posted @ 2014-03-20 17:45  十二月雨  阅读(1114)  评论(0编辑  收藏  举报