With this code (with or without the ".ToList()"):
使用此代码(带有或没有“.ToList()”):
public async Task<List<String>> SelectDistinctGroupNames()
{
var db = new SQLiteAsyncConnection(SQLitePath);
var groupNames = await db.QueryAsync<List<string>>("SELECT DISTINCT GroupName FROM SOs_Locations");
return groupNames.ToList();
}
...I get, "Cannot implicitly convert type 'System.Collections.Generic.List>' to 'System.Collections.Generic.List'"
…我得到,“不能隐式地转换类型的系统。集合。通用。”列表>”到“System.Collections.Generic.List’”
This works:
如此:
public async Task<HashSet<String>> SelectDistinctGroupNames()
{
var db = new SQLiteAsyncConnection(SQLitePath);
var allLocations = await db.QueryAsync<SOs_Locations>("SELECT * FROM SOs_Locations ORDER BY GroupName");
HashSet<string> hashsetGroupNames = null;
foreach (var item in allLocations)
{
hashsetGroupNames.Add(item.GroupName);
}
return hashsetGroupNames;
}
...but seems wasteful (grabbing all the records, when all I want is distinct values from the GroupName column).
…但是看起来很浪费(当我想要从GroupName列中得到不同的值时,获取所有的记录)。
It seems to me that what is needed is a List or even a HashSet when replacing the "*" in the sql query with a "DISTINCT GroupName"
在我看来,需要的是一个列表,甚至是一个HashSet,当用一个“不同的GroupName”替换sql查询中的“*”时
So what exactly is returned when a single column from several records is returned? IOW, what should be within the angle brackets of the call to QueryAsync<>() ?
那么,当返回多个记录的单个列时,返回的究竟是什么?在调用QueryAsync<>()的尖括号内应该是什么?
I would think that this would work:
我认为这是可行的:
public async Task<List<String>> SelectDistinctGroupNames()
{
var db = new SQLiteAsyncConnection(SQLitePath);
List<string> allLocations = await db.QueryAsync<string>("SELECT DISTINCT GroupName FROM SOs_Locations ORDER BY GroupName");
return allLocations;
}
...but with that I get, "'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.SQLiteAsyncConnection.QueryAsync(string, params object[])'"
…但是,我得到的是,“string”必须是一个非抽象类型,使用公共无参数构造函数,以便在泛型类型或方法的SQLite.SQLiteAsyncConnection中使用它作为参数“T”。QueryAsync(字符串,params对象[])”
I had a "string" above to correspond with <SOs_Locations
> (not "List<SOs_Locations
>") in the working version of the method. When I change it to "List<string
>", I get, "Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.List
' to 'System.Collections.Generic.List<string
>'"
在该方法的工作版本中,我有一个“字符串”,以对应于< sos_location >(不是“List< sos_location >”)。当我将其更改为“List
2 个解决方案
#1
6
From the source:
从源:
public Task<List<T>> QueryAsync<T> (string sql, params object[] args)
where T : new ()
So if you want List<string>
as a result, you would need to pass string
for T
. As you noticed, string
cannot satisfy a new()
constraint (because it does not have a parameterless constructor), so that's not possible.
因此,如果您想要列表
I took a look through the code and it looks to me like the new()
constraint is unnecessary, so my first stop would be with the sqlite-net folks asking them to remove it (and verifying that T=string
will work).
我查看了代码,它看起来像新的()约束是不必要的,所以我的第一站是sqlite-net的人要求他们删除它(并验证T=string将工作)。
In the meantime, you should be able to create a type for the result of this query:
同时,您应该能够为这个查询创建一个类型:
public sealed class DistinctGroupNamesResult
{
public string GroupName { get; set; }
}
and use a brief transformation:
并使用一个简短的转换:
public async Task<List<String>> SelectDistinctGroupNames()
{
var db = new SQLiteAsyncConnection(SQLitePath);
var result = await db.QueryAsync<DistinctGroupNamesResult>("SELECT DISTINCT GroupName FROM SOs_Locations");
return result.Select(x => x.GroupName).ToList();
}
There may be simpler solutions, e.g., using the higher-level LINQ provider (not sure if it supports DISTINCT
, though). Or it may be possible to use SOs_Locations
as the result type instead of DistinctGroupNamesResult
.
可能有更简单的解决方案,例如,使用更高级别的LINQ提供者(但不确定是否支持不同的)。或者,可以使用sos_location作为结果类型,而不是特殊的groupnamesresult。
#2
5
List<String> resultString = new List<string>();
var result = await db.QueryAsync<DistinctGroupNamesResult>("SELECT * FROM SOs_Locations");
foreach (string item in result.Select(x=> x.GroupName).Distinct())
{
resultString.Add(item);
}
return resultString;
#1
6
From the source:
从源:
public Task<List<T>> QueryAsync<T> (string sql, params object[] args)
where T : new ()
So if you want List<string>
as a result, you would need to pass string
for T
. As you noticed, string
cannot satisfy a new()
constraint (because it does not have a parameterless constructor), so that's not possible.
因此,如果您想要列表
I took a look through the code and it looks to me like the new()
constraint is unnecessary, so my first stop would be with the sqlite-net folks asking them to remove it (and verifying that T=string
will work).
我查看了代码,它看起来像新的()约束是不必要的,所以我的第一站是sqlite-net的人要求他们删除它(并验证T=string将工作)。
In the meantime, you should be able to create a type for the result of this query:
同时,您应该能够为这个查询创建一个类型:
public sealed class DistinctGroupNamesResult
{
public string GroupName { get; set; }
}
and use a brief transformation:
并使用一个简短的转换:
public async Task<List<String>> SelectDistinctGroupNames()
{
var db = new SQLiteAsyncConnection(SQLitePath);
var result = await db.QueryAsync<DistinctGroupNamesResult>("SELECT DISTINCT GroupName FROM SOs_Locations");
return result.Select(x => x.GroupName).ToList();
}
There may be simpler solutions, e.g., using the higher-level LINQ provider (not sure if it supports DISTINCT
, though). Or it may be possible to use SOs_Locations
as the result type instead of DistinctGroupNamesResult
.
可能有更简单的解决方案,例如,使用更高级别的LINQ提供者(但不确定是否支持不同的)。或者,可以使用sos_location作为结果类型,而不是特殊的groupnamesresult。
#2
5
List<String> resultString = new List<string>();
var result = await db.QueryAsync<DistinctGroupNamesResult>("SELECT * FROM SOs_Locations");
foreach (string item in result.Select(x=> x.GroupName).Distinct())
{
resultString.Add(item);
}
return resultString;