Search code examples
c#linqlinq-to-entities

Reconfigure Linq query to create objects: LINQ to Entities does not recognize the method


I have the following query that return a new set of objects:

  _MapData.features = (from gs in QBEntities.GeoStates
                       select new MapDataRecord()
                       {
                         properties = new MapDataRecordProperties()
                                      {
                                        GEOID = gs.GEOID,
                                        GEO_NAME = gs.GEO_NAME
                                      },
                         geometry = SetGeoJsonGeography(gs.GEO_OBJECT.SpatialTypeName, gs.JSON_GEOMETRY)
                       }
                      ).ToList();

However it has a problem with SetGeoJsonGeography(), I am getting the error: 'LINQ to Entities does not recognize the method'

Before I create the geometry it needs to figure out the geometry type so it can make the right type of array.

The method:

private MapDataGeometry SetGeoJsonGeography(string GeographyType, string GeoJsonGeographyString)
{
  if (GeographyType.Equals("polygon", StringComparison.CurrentCultureIgnoreCase))
  {
    return new MapDataPolygon() { type = GeographyType, coordinates = Newtonsoft.Json.JsonConvert.DeserializeObject<double[][][]>(GeoJsonGeographyString) };
  }
  else if (GeographyType.Equals("multipolygon", StringComparison.CurrentCultureIgnoreCase))
  {
    return new MapDataMultiPolygon() { type = GeographyType, coordinates = Newtonsoft.Json.JsonConvert.DeserializeObject<double[][][][]>(GeoJsonGeographyString) };
  }
  else
  {
    return null;
  }
}

Here are the classes:

  [Serializable]
  public class MapDataGeometry
  {
    public string type { get; set; }
  }

  [Serializable]
  public class MapDataPolygon : MapDataGeometry
  {
    public double[][][] coordinates { get; set; }
  }

  [Serializable]
  public class MapDataMultiPolygon : MapDataGeometry
  {
    public double[][][][] coordinates { get; set; }
  }

How can I achieve this?


Solution

  • Your method cannot be transferred to your sql provider. You should get the raw data first and execute your method after that.

    var geoStates = (from gs in QBEntities.GeoStates
                     select new
                     {
                         gs.GEOID,
                         gs.GEO_NAME,
                         SpatialTypeName = gs.GEO_OBJECT.SpatialTypeName,
                         gs.JSON_GEOMETRY
                     }).ToList();
    
    _MapData.features = (from gs in geoStates
                         select new MapDataRecord
                         {
                             properties = new MapDataRecordProperties
                             {
                                 GEOID = gs.GEOID,
                                 GEO_NAME = gs.GEO_NAME
                             },
                             geometry = SetGeoJsonGeography(gs.SpatialTypeName, gs.JSON_GEOMETRY)
                         }).ToList();