0

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?

Martin Schneider
  • 14,263
  • 7
  • 55
  • 58
  • 1
    That first error means you're referring to an object that hasn't been defined in that scope, but you haven't shown the code that's generating the error. The second one means you have to pass some arguments to `OrderBy` to specify what properties you want to use for ordering. For example, if you have a list of `CalendarEntry` objects named `calendarEntries`, you could do: `return calendarEntries.OrderBy(ce => ce.CalendarEntryStartDate)` – Rufus L Jul 08 '19 at 19:11

1 Answers1

5

OrderBy expects a lambda expression as an argument

OrderBy(x => x.CalendarEntryStartDate)
Jason
  • 86,222
  • 15
  • 131
  • 146