I am still learning the MVVM pattern and am making a new project(C#4.0 & WPF) which involves SQLite databases. I haven’t used databases(sql) much in the past, but what I would do is something like this:
Select * from persons;
then loop through the result and foreach result add it in the persons class, something like
result[0] = person.name;
result[1] = person.id;
et cetera. Then I would use a for loop until I went through all the rows and added all the players to a List (or ObservableCollection for MVVM). Now I want to be more dynamic, since I’ll have many more rows I am looking for something like:
Person personone = "Select * from persons where ID = 1";
But this doesn’t work, of course. I have been tried searching for my question on Google but have not found an answer. I found SQL to LINQ but it is not really the thing I am looking for. The most relevant thing I found is the massive sqlite class, but it doesn’t seem to work with VS11 as it appears to have been deprecated.
How can I more dynamically insert data from SQLite into a class?
Dapper is a nice solution for this. You can map the results of your query to custom POCOs, or to dynamic objects.
Mapping the query results to an object is as simple as:
For a single instance, you would do this: