Search code examples
c#sqlitexamarin.formssqlite-net-pcl

How to use ".OrderBy" in Xamarin.Forms with SQLite-net-plc?


I want to order my calendar entries I save in an SQLite Database to show them in a Listview. How can I order them with SQLite-net-plc?

I thought that I could just write: .OrderBy<CalendarEntryStartDate> but it didn't work and I tried to use an SQLite Command but I had some trouble with using them as I was confused by the 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<>;
        }
    }
}

I got the errors

CS0103: The name "CalendarEntryStartDate" does not exist in the current context;

and

CS0305: The Usage of the method group "OrderBy"(generic) need 1-Typeargumetns.

What am I doing wrong?


Solution

  • OrderBy expects a lambda expression as an argument

    OrderBy(x => x.CalendarEntryStartDate)