首页 > 解决方案 > 如何通过 SQLite-net-plc 在 Xamarin.Forms 中使用“.OrderBy”?

问题描述

我想订购我保存在 SQLite 数据库中的日历条目,以便在 Listview 中显示它们。如何使用 SQLite-net-plc 订购它们?

我以为我可以写:.OrderBy<CalendarEntryStartDate>但它不起作用,我尝试使用 SQLite 命令,但我在使用它们时遇到了一些麻烦,因为我对 m.db 感到困惑。

using System;
using SQLite;

namespace Stundenplan.Models
{
    public class CalendarEntry
    {
        [PrimaryKey, AutoIncrement]
        public int CalendarEntryId { get; set; }
        public string CalendarEntryTitle { get; set; }
        public string CalendarEntryDescription { get; set; }
        [Column("StatDate")]
        public DateTime CalendarEntryStartDate { get; set; }
        public DateTime CalendarEntryEndDate { get; set; }
        public TimeSpan CalendarEntrySpan { get; set; }
        public string CalendarEntryParticipants { get; set; }
        public string CalendarEntrytLocation { get; set; }
        public Boolean CalendarEntryPrivate { get; set; }
        public string CalendarEntryTags { get; set; }
        public string CalendarEntryColorTag { get; set; }
    }
}


using SQLite;
using System.Collections.Generic;
using Stundenplan.Models;
using System.Threading.Tasks;

namespace Stundenplan.Data
{
    public class CalendarEntryDatabase
    {
        readonly SQLiteAsyncConnection calendarentrydatabase;

        public CalendarEntryDatabase(string dbPath)
        {
            calendarentrydatabase = new SQLiteAsyncConnection(dbPath);
            calendarentrydatabase.CreateTableAsync<CalendarEntry>().Wait();
        }
        public Task<List<CalendarEntry>> GetCalendarEntrysAsync()
        {
            return calendarentrydatabase.Table<CalendarEntry>().ToListAsync();
        }
        public Task<CalendarEntry> GetCalendarEntryAsync(int id)
        {
            return calendarentrydatabase.Table<CalendarEntry>().Where(i => i.CalendarEntryId == id).FirstOrDefaultAsync();
        }
        public Task<int> SaveCalendarEntryAsync(CalendarEntry calendarentry)
        {
            if (calendarentry.CalendarEntryId == 0)
            {
                return calendarentrydatabase.InsertAsync(calendarentry);
            }
            else
            {
                return calendarentrydatabase.UpdateAsync(calendarentry);
            }
        }
        public Task<int> DeleteCalendarEntryAsync(CalendarEntry calendarentry)
        {
            return calendarentrydatabase.DeleteAsync(calendarentry);
        }
        public Task<List<CalendarEntry>> GetCalendarEntriesOrderedByStartDateAsync()
        {
            return calendarentrydatabase.Table<CalendarEntry>().OrderBy<>;
        }
    }
}

我得到了错误

CS0103:当前上下文中不存在名称“CalendarEntryStartDate”;

CS0305:方法组“OrderBy”(通用)的使用需要 1-Typeargumetns。

我究竟做错了什么?

标签: c#sqlitexamarin.formssqlite-net-pcl

解决方案


OrderBy期望lambda表达式作为参数

OrderBy(x => x.CalendarEntryStartDate)

推荐阅读