如何只返回一个列值而不是整个C#类/ SQLite表? [英] How can I return just one column value rather than the entire C# class/SQLite table?

查看:152
本文介绍了如何只返回一个列值而不是整个C#类/ SQLite表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinRT应用程序中有这个代码:



I have this code in my WinRT app:

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:

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





我最终得到的是一个列出的项目列表框:Photrax.Model.PhotraxBaseData



我想要的只是一列(photosetName),但当我试图返回List< String>而不是List< PhotraxBaseData>像这样:





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:

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;
}





...我知道,在这一行:psets = db.Query< String>(sql) ;,以下编译时间错误消息:



'string'必须是非抽象类型,带有公共无参数构造函数才能使用它作为参数'T'在泛型类型或方法'SQLite.SQLiteConnection.Query< T>(string,params object [])'



所以我怎样才能做到这一点?



...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?

推荐答案

在原始版本中,您不能只使用LINQ选择所需的列。将列的名称更改为适当的列。

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;


这篇关于如何只返回一个列值而不是整个C#类/ SQLite表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆