سؤال

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>();
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

If RegionName is a varchar then all you need is

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

You don't need the <string>

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top