重新配置 Linq 查询以创建对象:LINQ to Entities 无法识别该方法

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

我有以下查询 return 一组新对象:

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

但是 SetGeoJsonGeography() 有问题,我收到错误:'LINQ to Entities does not recognize 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;
  }
}

这是类:

  [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; }
  }

我怎样才能做到这一点?

您的方法无法转移到您的 sql 提供商。 您应该先获取原始数据,然后再执行您的方法。

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