Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This query returns 4 records (2x is IdStandard = 2) - I need 3 records with unique IdStandard and PrimaryCalcMethod and SecondaryCalcMethod methods. If the latter method is not present, the SecondaryCalcMethod property is to return null. How to change the linq query?

System.NotSupportedException: LINQ to Entities does not recognize the method 'AppForSellers.Models.BusinessLogic.SpiralStairs.Models.DiamDivide ParseDiamDivide' method, and this method cannot be translated into a store expression.

What I have tried:

C#
public List<standardent> ListStandardsWithMethodList()
{
    var listModel =
        (
        from dane in _dbContext.Standard
        join kraj in _dbContext.CountryStandard
        on dane.IdCountryStandard equals kraj.IdCountryStandard
        join opcja in _dbContext.MeasureDiameterOption
        on dane.IdStandard equals opcja.IdStandard
        join metoda in _dbContext.MeasureDiameterMethod
        on opcja.IdMeasureDiameterMethod equals metoda.IdMeasureDiameterMethod
        join sposob in _dbContext.WalkingLineRadius
        on opcja.IdWalkingLineRadius equals sposob.IdWalkingLineRadius
        join linia in _dbContext.CalcMethodType
        on sposob.IdCalcMethodType equals linia.IdCalcMethodType
        join wyznaczanie in _dbContext.WalkingLineMeasure
        on sposob.IdWalkingLineMeasure equals wyznaczanie.IdWalkingLineMeasure
        select new StandardEnt()
        {
            IdStandard = dane.IdStandard,
            IdCountryStandard = dane.IdCountryStandard,
            StandardName = dane.StandardName,
            IsActive = dane.IsActive,
            NameCountry = kraj.NameCountryStandard,
            FreeHeadSpace = (double)dane.FreeHeadSpace,
            IsTlcStandard = dane.IsTlcStandard,
            MinLandingSize = (double)dane.MinLandingSize,
            MinStepHeight = (double)dane.MinStepHeight,
            MaxStepHeight = (double)dane.MaxStepHeight,
            MinConvinience = (double)dane.MinConvinience,
            MaxConvinience = (double)dane.MaxConvinience,
            MaxStepDeep = (double)dane.MaxStepDeep,
            MinStepDeep = (double)dane.MinStepDeep,
            MaxStepWidth = (double)dane.MaxStepWidth,
            WidthLimit = (double)dane.WidthLimit,
            StartOverlap = (double)dane.StartOverlap,
            InternalHandrailMaxRadius = (double)dane.InternalHandrailMaxRadius,
            PrimaryCalcMethod = (from metody in _dbContext.MeasureDiameterOption
                                 where dane.IdStandard == metody.IdStandard && metody.MeasureDiameterMethod.CalcMethodName == "PrimaryCalcMethod"
                                 select new MeasureDiameterMethodEnt()
                                 {
                                     IdMeasureDiameterOption = opcja.IdMeasureDiameterOption,
                                     Description = opcja.Description,
                                     IdMeasureDiameterMethod = metoda.IdMeasureDiameterMethod,
                                     IdWalkingLineRadius = sposob.IdWalkingLineRadius,
                                     IdCalcMethodType = linia.IdCalcMethodType,
                                     IdWalkingLineMeasure = wyznaczanie.IdWalkingLineMeasure,
                                     CalcMethod = Enum.Parse<diamdivide>(linia.CalcMethodNameType),
                                     WalkingLineMeasure = Enum.Parse<walkinglinemeasuremethod>(wyznaczanie.WalkingLineMeasureName),
                                     CalcMethodName = metoda.CalcMethodName,
                                     FirstWidth = (double)sposob.FirstWidth,
                                     SecondWidth = (double)sposob.SecondWidth
                                 }).FirstOrDefault(),
            SecondaryCalcMethod = (from metody in _dbContext.MeasureDiameterOption
                                   where dane.IdStandard == metody.IdStandard && metody.MeasureDiameterMethod.CalcMethodName == "SecondaryCalcMethod"
                                   select new MeasureDiameterMethodEnt()
                                   {
                                       IdMeasureDiameterOption = opcja.IdMeasureDiameterOption,
                                       Description = opcja.Description,
                                       IdMeasureDiameterMethod = metoda.IdMeasureDiameterMethod,
                                       IdWalkingLineRadius = sposob.IdWalkingLineRadius,
                                       IdCalcMethodType = linia.IdCalcMethodType,
                                       IdWalkingLineMeasure = wyznaczanie.IdWalkingLineMeasure,
                                       CalcMethod = Enum.Parse<diamdivide>(linia.CalcMethodNameType),
                                       <code></code>                                               WalkingLineMeasure = Enum.Parse<walkinglinemeasuremethod>(wyznaczanie.WalkingLineMeasureName),
                                       CalcMethodName = metoda.CalcMethodName,
                                       FirstWidth = (double)sposob.FirstWidth,
                                       SecondWidth = (double)sposob.SecondWidth
                                   }).FirstOrDefault()
        }
    ).ToList();
    return listModel;
}
Posted
Updated 6-Apr-21 11:13am
v2
Comments
[no name] 18-Mar-21 15:43pm    
Write "intermediate" queries that string together so it's easier to "debug".

1 solution

Whenever you get a `LINQ to Entities does not recognize the method abc`, it indicates that you're trying to use some method that can't be converted into SQL. From the looks of it, it could be this:

CalcMethod = Enum.Parse<diamdivide>(linia.CalcMethodNameType),


Instead of calling `Enum.Parse(...)` in your LINQ-to-entities (which of course can't be converted to SQL), try just returning `linia.CalcMethodNameType`. Then, after calling `ToList()` once (which will execute the SQL), parse the enum and return the result.

As a general rule, whenever you need to to complex mapping or parsing, you'll likely have a 2-step process. The first step is to read the data from the database, and call `ToList()` (or equivalent). The second step is to do the mapping / parsing on the result.

Hope that made sense.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900