1

我在 Microsoft Dynamics CRM 2011 中有一个情况,这里我有一组帐户,其中包含与 permitCounty 相关的 Permit 和 permit ae,并且 permitCounty 与 County

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

但这不合适

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();

任何帮助表示赞赏。

维努

4

1 回答 1

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();
于 2012-10-19T19:15:29.113 回答