Linq查询未由Entity Framework处理

时间:2018-02-13 07:32:35

标签: c# entity-framework linq

我一直在寻找 LINQ to Entities无法识别广泛讨论的方法主题。听起来我的C#代码无法被翻译成由实体处理的查询...问题是我对这个框架不够好,无法理解真正的工作方式。 任何人都可以帮助我理解为了修复我的代码必须更改的内容吗?

我选择受益人(人/公司)并关联他们的姓名。

以下是代码:

 var context = Context as OracleDataContextCommon;
            List<Dictionary<Beneficiary, string>> BeneficiaryList = new List<Dictionary<Beneficiary, string>>();
            Dictionary<Beneficiary, string> BeneficiaryPerson = new Dictionary<Beneficiary, string>();
            Dictionary<Beneficiary, string> BeneficiaryCompany = new Dictionary<Beneficiary, string>();

            //Let's select all persons that are not listed as Health Partner
            BeneficiaryPerson = (from bnf in context.Beneficiary
                         where bnf.HmcBen == 0
                         from pers in context.Person
                         where pers.PersonId == bnf.PerId
                         orderby pers.Lastname
                         group new { bnf, pers } by new { bnf.LUP_DATE,  pers.PersonId} into grp
                         let earliest = grp.Max(o => o.bnf.LUP_DATE)
                         select new
                         {
                             beneficiary = grp.Where(x => x.bnf.LUP_DATE == earliest).Select(x => x.bnf).FirstOrDefault(),
                             name = string.Format("{0} {1}", grp.Select(x => x.pers.Firstname).FirstOrDefault(), grp.Select(x => x.pers.Lastname).FirstOrDefault())
                         })
                         .ToDictionary(x => new Beneficiary() {
                             BnfId = x.beneficiary.BnfId,
                             CodeBic = x.beneficiary.CodeBic,
                             IbanCode = x.beneficiary.IbanCode,
                             PerId = x.beneficiary.PerId,
                             BnkId = x.beneficiary.BnkId,
                             HmcBen = x.beneficiary.HmcBen
                         }, x => x.name);

            //Let's select all companies that are not listed as Health Partner
            BeneficiaryCompany = (from bnf in context.Beneficiary
                                where bnf.HmcBen == 0
                                from cie in context.Company
                                where cie.CompanyId == bnf.CieId
                                orderby cie.CompanyName
                                group new { bnf, cie } by new { bnf.LUP_DATE, cie.CompanyId} into grp
                                let earliest = grp.Max(o => o.bnf.LUP_DATE)
                                select new
                                {
                                    beneficiary = grp.Where(x => x.bnf.LUP_DATE == earliest).Select(x => x.bnf).FirstOrDefault(),
                                    name = grp.Select(x => x.cie.CompanyName).FirstOrDefault()
                                })
                                .ToDictionary(x => new Beneficiary(){
                                    BnfId = x.beneficiary.BnfId,
                                    CodeBic = x.beneficiary.CodeBic,
                                    IbanCode = x.beneficiary.CodeBic,
                                    CieId = x.beneficiary.CieId,
                                    BnkId = x.beneficiary.BnkId,
                                    HmcBen = x.beneficiary.HmcBen
                                }, x => x.name );

            BeneficiaryList.Add(BeneficiaryPerson);
            BeneficiaryList.Add(BeneficiaryCompany);

            return BeneficiaryList;

1 个答案:

答案 0 :(得分:3)

实体框架 / Linq to Sql 无法将String.Format转换为SQL ..简而言之,除非你使用它知道你在记忆中