Left Outer在CRM 2011中使用四个实体加入LINQ查询,并使用多个where子句

时间:2012-10-19 17:18:43

标签: linq linq-to-sql linq-to-entities dynamics-crm-2011

我遇到过Microsoft Dynamics CRM 2011的情况 在这里,我有一套帐户,其中有许可证和许可证与permitCounty相关,而许可证县与县有关

我想得到像这个查询的输出。

但这不合适

strSearch="Some text";

var lstItems = (from county in x.De_countySet
                join permitcounty in x.de_permitcountySet on county.De_countyId equals permitcounty.de_CountyId.Id
                join permit in x.de_permitSet on permitcounty.de_PermitId.Id equals permit.de_permitId
                join account in x.AccountSet on permit.de_AccountId.Id equals account.AccountId into outer

                where permit.de_Type.Equals("Type1") && permit.statuscode.Equals("Active")                // Permit Type as Type1 and Status as Active
                where account.De_LegalName.Contains(strSearch) || account.Name.Contains(strSearch)        // Search in Legal Name and Account full name
                where county.de_CountyName.Contains(strSearch)                                            // Search in County Name

                orderby county.de_CountyName
                from account in outer.DefaultIfEmpty()
                select new clsTemp
                {
                    Name = account.Name,
                    De_LegalName = account.De_LegalName,
                    Address1_Line1 = account.Address1_Line1,
                    Address1_City = account.Address1_City,
                    Address1_PostalCode = account.Address1_PostalCode,

                    de_CountyName = county.de_CountyName              
                 }).ToList();

感谢任何帮助。

Vinu

1 个答案:

答案 0 :(得分:0)

这是编写组连接的另一种方法。请注意,包含into的行是唯一被替换的行:

strSearch="Some text";

var lstItems = (from county in x.De_countySet
                join permitcounty in x.de_permitcountySet on county.De_countyId equals permitcounty.de_CountyId.Id
                join permit in x.de_permitSet on permitcounty.de_PermitId.Id equals permit.de_permitId
                let outer = x.AccountSet.Where(account => permit.de_AccountId.Id == account.AccountId)

                where permit.de_Type.Equals("Type1") && permit.statuscode.Equals("Active")                // Permit Type as Type1 and Status as Active
                // "account" is not in scope and should cause a design-time error
                where account.De_LegalName.Contains(strSearch) || account.Name.Contains(strSearch)        // Search in Legal Name and Account full name
                where county.de_CountyName.Contains(strSearch)                                            // Search in County Name

                orderby county.de_CountyName
                from account in outer.DefaultIfEmpty()
                select new clsTemp
                {
                    Name = account.Name,
                    De_LegalName = account.De_LegalName,
                    Address1_Line1 = account.Address1_Line1,
                    Address1_City = account.Address1_City,
                    Address1_PostalCode = account.Address1_PostalCode,

                    de_CountyName = county.de_CountyName              
                 }).ToList();

当以这种方式编写时,会出现明显的错误(account不在范围内)。也许还有其他问题。没有任何许可证的县将不会显示,而具有多个许可证但没有任何帐户的县将有多个具有空白帐户的行。这是你想要的功能吗?

编辑:第二次尝试:

strSearch="Some text";

var lstItems = (from county in x.De_countySet
                join permitcounty in x.de_permitcountySet on county.De_countyId equals permitcounty.de_CountyId.Id
                join permit in x.de_permitSet on permitcounty.de_PermitId.Id equals permit.de_permitId


                where permit.de_Type.Equals("Type1") && permit.statuscode.Equals("Active")                // Permit Type as Type1 and Status as Active

                where county.de_CountyName.Contains(strSearch)                                            // Search in County Name

                orderby county.de_CountyName
                from account in x.AccountSet.Where(account => 
                    permit.de_AccountId.Id == account.AccountId &&
                    (account.De_LegalName.Contains(strSearch) || 
                     account.Name.Contains(strSearch))).DefaultIfEmpty()   // Search in Legal Name and Account full name
                select new clsTemp
                {
                    Name = account.Name,
                    De_LegalName = account.De_LegalName,
                    Address1_Line1 = account.Address1_Line1,
                    Address1_City = account.Address1_City,
                    Address1_PostalCode = account.Address1_PostalCode,

                    de_CountyName = county.de_CountyName              
                 }).ToList();