jeudi 24 septembre 2015

C# generic SQLite Where query Methode

how can I make this Method generic to save duplicate Code?

 private async Task<int> CheckCategorieDescription(string description)
    {
        try
        {
            var db = new SQLiteAsyncConnection(pathToDatabase);

            var query = db.Table<Categorie>().Where(v => v.Description.Contains(description));
            var list = await query.ToListAsync();

            return list.Count;
        }
        catch (SQLiteException ex)
        {
            return -1;
        }
    }

It's will by great, when to Code will be looks like.

 private async Task<int> CheckDescription<T>(string description)
    {
        try
        {
            var db = new SQLiteAsyncConnection(pathToDatabase);

            var query = db.Table<T>().Where(v => v.Description.Contains(description));
            var list = await query.ToListAsync();

            return list.Count;
        }
        catch (SQLiteException ex)
        {
            return -1;
        }
    }

Is there a method to insert the type of T in "db.Table" this Position.

Thanks for help

Aucun commentaire:

Enregistrer un commentaire