0

I am using Xamarin c# linq with sqlite-net pcl (https://github.com/praeclarum/sqlite-net). I found that all my linq groupby cannot be properly translated into SQL. Linq translates the Where clause but not Group By.

In the following example, the fields used in Transaction:

AccountId: int
Amount: double
ICategoryId: int

All these query formats result in SQL without Group By:

select * from "Transaction" where ("DateWithoutTime" <= ?)

var accountBalances1 = _dataService.Connection.Table<Transaction>()
    .Where(r => r.DateWithoutTime <= displayDuration.DurationEnd)
    .GroupBy(r => r.AccountId)
    .Select(g => new
    {
        Id = g.Key,
        Balance = g.Sum(b => b.Amount)
    })
    .ToList();

var accountBalances2 = _dataService.Connection.Table<Transaction>()
   .Where(r => r.DateWithoutTime <= displayDuration.DurationEnd)
   .GroupBy(r => r.AccountId,
    (key, g) => new
    {
        Id = key,
        Balance = g.Sum(b => b.Amount),
    })
    .ToList();

var accountBalances3 = (from t in _dataService.Connection.Table<Transaction>()
                        where t.DateWithoutTime <= displayDuration.DurationEnd
                        group t by t.AccountId
                        into g
                        select new { g.Key, Balance = g.Sum(g => g.Amount) })
                       .ToList();

To clarify it has nothing to do with double data type, I tried another group by with int data type only:

var maxIECategoryId = _dataService.Connection.Table<Transaction>()
   .Where(r => r.DateWithoutTime <= displayDuration.DurationEnd)
   .GroupBy(r => r.AccountId,
    (key, g) => new
    {
        Id = key,
        IECategoryId = g.Max(b => b.IECategoryId)
    })
    .ToList();

Similarly, the generated sql does not have group by.

All group by are processed locally. Is there any trick to write a linq group by that can be processed on the server service? or it is a limitation of this implementation of sqlite orm?

Thanks,

Nick

4
  • 1
    The source code says "A queryable object that is able to translate Where, OrderBy, and Take queries into native SQL." GroupBy does not appear to be supported, so you would have to switch to client side evaluation by putting AsEnumerable() before the GroupBy call. Commented Apr 26, 2021 at 20:11
  • @NetMage Thanks. After looked at the source code, I found that I am too naive to count on the simple orm. Commented Apr 27, 2021 at 9:53
  • @Nick The problem is not with sqlite-net that is a light, very simple and efficient Code-First ORM. You work with Lists. You read tables or SQLite queries to get lists, you update db from lists, you insert and delete in db while mainaining lists, unless using the sqlite-net-extensions that is very slow on large foreigns. That's all. Some things must be done manually but it's not much compared to the gain on simple apps. If there is a problem with groupby it is with the code on the .net generic lists itself. I'm not advanced in groupby and had not read your code, thus I can't help more, sorry. Commented Jun 3, 2021 at 13:13
  • @nick In other words, using sqlite-net, you work with LINQ-to-objects, not LINQ-toSQL. No SQLite SQL query is generated while using LINQ on sqlite-net. That its power and its blemish. And I really like to use it with the BindingListView. Commented Jun 3, 2021 at 13:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.