Search code examples
simple.data

Simple.Data casting to ToList<string>()


I am using Simple.Data and want to know if I can Select a single column and then cast it to a list of string values. For example using the query below I get the error:

Cannot implicitly convert type 'Simple.Data.SimpleRecord' to 'string'

var result = _database.ParentRegionList.All()
            .Select(_database.ParentRegionList.RegionName)
            .Where(_database.ParentRegionList.RegionName.Like(startsWith + "%"))
            .Distinct()
            .ToList<string>();

However if I create a class LocationAutoComplete that has a single public property "RegionName" of type string then the cast works fine.

var result = _database.ParentRegionList.All()
            .Select(_database.ParentRegionList.RegionName)
            .Where(_database.ParentRegionList.RegionName.Like(startsWith + "%"))
            .Distinct()
            .ToList<LocationAutoComplete>();

Solution

  • The ToList<T> Simple.Data method expects you to be casting the contents of a SimpleRecord to an object, which is why it works with your LocationAutoComplete class. Full details can be found here.

    If you are returning only one field which you wish to return as a scalar value or a list of scalar values, use the ToScalar<T> or ToScalarList<T> method instead. Full details can be found here