0

I've been searching for LINQ to Entities does not recognize the method topics, which are widely discussed. It sounds like my C# code can't be translated into a query handled by Entity... Problem is I'm not good enough with this framework to understand how it really works. Could anyone help me with understanding what must be changed in order to fix my code ?

I'm selecting beneficiaries (persons / companies) and associating their name.

Here is the code :

 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;
Florian
  • 1,473
  • 10
  • 15

1 Answers1

3

Entity Framework / Linq to Sql can't convert String.Format to SQL.. in short, don't use it unless you know you are in memory

TheGeneral
  • 79,002
  • 9
  • 103
  • 141