I have a database table with a field that has values such as
AAAA
BBBB0001
BBBB0002
CCCC
CCCC
CCCC
and I would like to know how many AAAAs, BBBBs and CCCCs there are (1, 2 and 3 here).
If I set up a couple of test queries in LinqPad with those values in an array:
var Table = new [] {"AAAA", "BBBB1", "BBBB2", "CCCC", "CCCC", "CCCC"};
var query1 = Table.GroupBy(d => d).Select( g => new { K = g.Key, C = g.Count()});
query1.Dump();
var query2 = Table.GroupBy(d => d.Substring(0, 4)).Select( g => new { K = g.Key, C = g.Count()});
query2.Dump();
then I get the results I expect – in the first case grouped by the key and in the second grouped by the first four characters of the key
AAAA 1
BBBB1 1
BBBB2 1
CCCC 3
AAAA 1
BBBB 2
CCCC 3
which is fine. But if replace the array by a SQLite table and run the same queries, what I get is
AAAA 1
BBBB1 1
BBBB2 1
CCCC 3
AAAA 1
BBBB 1
BBBB 1
CCCC 3
That is, the first is correct, while in the second, the field is being correctly shortened but the grouping is wrong.
Anyone cast any light on this?
Andrew
What connector are you using for your linq to sqlite? It feels like it’s likely to be a bug in that connector.
e.g. Looking at the revision table for dotconnect – http://www.devart.com/dotconnect/sqlite/revision_history.html – then there are plenty of group by bugs that they’ve found and fixed in recent months – and I’m sure there could be others in there.
Unless you absolutely need sqlite, then you could consider switching to SQL CE – in which case you’d get more direct support for Linq from Microsoft.