【发布时间】:2020-07-14 10:19:15
【问题描述】:
我正在学习 Xamarin,我正在使用这个 [sqlite 插件][1.我想按日期(MyCreateDate)汇总表值,并将这些日期(moth,yeah)中的单词数(Word1)相加。
例如,如果我有:
3 words in November 2001, nothing in December 2001, 6 words in january 2002,2 words in febuary 2002, nothing in March 2002.
我希望得到类似的回报:
Moth Year NumberWords
11 2001 3
12 2001 0
1 2002 6
2 2002 2
3 2002 0
这是我的模型 Model.cs:
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
[JsonProperty(PropertyName = "id")]
public string MyId { get; set; }
[JsonProperty(PropertyName = "myCreated")]
public DateTime MyCreateDate { get; set; } = DateTime.UtcNow; // with this value of date
[JsonProperty(PropertyName = "myUpdate")]
public DateTime MyUpdateDate { get; set; } = DateTime.UtcNow;
[JsonProperty(PropertyName = "complete")]
public bool Complete { get; set; }
[JsonProperty(PropertyName = "wordissaved")]
public bool WordIsSaved { get; set; } = false;
[JsonProperty(PropertyName = "word1")]
public string Word1 { get; set; }
[JsonProperty(PropertyName = "word2")]
public string Word2 { get; set; }
这是数据库 MyWordsDatabase.cs,我已经设法获取单词并选择随机单词:
class MyWordsDatabase
{
private SQLiteAsyncConnection conn;
//CREATE
public MyWordsDatabase()
{
conn = DependencyService.Get<ISQLite>().GetConnection();
conn.CreateTableAsync<MyWords>();
}
//READ
public Task <List<MyWords>> GetWords()
{
var wordList = conn.Table<MyWords>();
return wordList.OrderByDescending(w => w.ID).ToListAsync();
}
// Select ramdon words and put them in a list
public Task<List<MyWords>> GetRandomListWords(int number)
{
var words = conn.QueryAsync<MyWords>("select DISTINCT * from MyWords ORDER BY RANDOM() LIMIT " + number + "");
return words;
}
}
感谢您的帮助
【问题讨论】:
-
为什么要使用字符串作为日期列?这让你想做的事情变得非常困难。
-
@Jason 你可以这样做,因为日期对我来说没问题
-
@Jason 我已经更新了代码
标签: c# sqlite xamarin.forms sqlite-net