Click here to Skip to main content
15,946,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code in my WinRT app:

C#
internal static List<PhotraxBaseData> GetPhotosets()
{
    List<PhotraxBaseData> psets = new List<PhotraxBaseData>();
    using (var db = new SQLite.SQLiteConnection(App.DBPath))
    {
        // Does SQLite support "DISTINCT"?
        string sql = "SELECT DISTINCT photosetName FROM PhotraxBaseData ORDER BY
photosetName";
        psets = db.Query<PhotraxBaseData>(sql);
    }
    return psets;
}


Which is called this way:

C#
private void flyoutOpenPhotosets_Opened(object sender, object e)
{
    lstbxPhotosets.ItemsSource = PhotraxSQLiteUtils.GetPhotosets();
}


What I end up with this is a listbox filed with items that say: "Photrax.Model.PhotraxBaseData"

All I want is just that one column (photosetName), but when I tried to return List<String> instead of List<PhotraxBaseData> like so:

C#
internal static List<String> GetPhotosets()
{
    List<String> psets = new List<String>();
    using (var db = new SQLite.SQLiteConnection(App.DBPath))
    {
        string sql = "SELECT DISTINCT photosetName FROM PhotraxBaseData ORDER BY
photosetName";
        psets = db.Query<String>(sql);
    }
    return psets;
}


...I get, on this line: "psets = db.Query<String>(sql);", the following compile-time err msg:

'string' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'SQLite.SQLiteConnection.Query<T>(string, params object[])'

So how can I accomplish this?
Posted

1 solution

In your original version can you not just select the required column(s) using LINQ. Change the name of the column to the appropriate one.
var setnames = (from a in psets select a.SetName).ToList();
return setnames;
 
Share this answer
 
Comments
B. Clay Shannon 30-Oct-14 13:19pm    
Thanks, I'll check that (can't know; I'm at work).

But wouldn't I still have the "'string' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'SQLite.SQLiteConnection.Query(string, params object[])'" compile-time err msg?
Dominic Burford 30-Oct-14 13:23pm    
Add the code to your first version of the function, NOT the one that uses the List<string>
B. Clay Shannon 30-Oct-14 13:35pm    
They both use a list; one a list of PhotraxBaseData, and the other a list of String.
Dominic Burford 31-Oct-14 4:05am    
Add it to the first code snippet you supplied where you return List<PhotraxBaseData>

string sql = "SELECT DISTINCT photosetName FROM PhotraxBaseData ORDER BY
photosetName";
psets = db.Query<PhotraxBaseData>(sql);

//filter your list here to return just the columns you require
var setnames = (from a in psets select a.SetName).ToList();
return setnames;
B. Clay Shannon 31-Oct-14 10:46am    
Thanks; I'm on the case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900