Our team is starting to develop WebApi's using Appeon's Snapobjects. One of the main libraries we are using is the SqlExecutor class to create our data access layer.
One of the things that got us confused was the ability to use the SqlExecutor.Select function to select values from one single column in a table. Take the example below:
string selectSql = "SELECT col_1 FROM table"; List<string> list = new List<string>(); var templist = _context.SqlExecutor.Select<DynamicModel>(selectSql); for (int i = 0; i < templist.Count; i++) { list.Add(templist[i].GetValue<string>("col_1")); }
The above code works as expected but before arriving at that solution we attempted to do this:
string selectSql = "SELECT col_1 FROM table";
List<string> list = new List<string>();
list = _context.SqlExecutor.Select<string>(selectSql);
OR
string selectSql = "SELECT col_1 FROM table";
List<string> list = new List<string>();
list = _context.SqlExecutor.Select<string>(selectSql) as List<string>;
OR
string selectSql = "SELECT col_1 FROM table";
List<string> list = new List<string>();
list = _context.SqlExecutor.Select<string>(selectSql).ToList();
All of the 3 examples above DO NOT work. I was just wondering why. Can we not use standard data types in the Select<TModel> field? It seems weird we have to manually populate a List<string> manually by looping through all of the values.
If you need to only get one column by executing a SQL in your project frequently, you can extend a custom method (Such as SelectFirstColumn() or SelectColumn("ColumnName")) on the SqlExecutor. You can also submit an enhancement requirement at https://www.appeon.com/standardsupport/.
Regards, Logan
Thanks!