linq orderby random | Unknown column in 'order clause'

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
Posts: 17
Joined: Wed 08 Jul 2009 16:31

linq orderby random | Unknown column in 'order clause'

Post by infinitep » Fri 16 Apr 2010 10:46

I am getting the following error trying to orderby random in linq dotconnect mysql using the following code;

Random rnd = new Random();
var accountsrandom = (from a in dc.accounts orderby rnd.Next() select a);
int count = accountsrandom.Count();
var accountsrandom2 = accountsrandom.Skip((Convert.ToInt16(4) * (PageNumber - 1)))
result = accountsrandom2.ToList();

and the error i get;

MySqlException (0x80004005): Unknown column '76316738' in 'order clause']
Devart.Data.MySql.bk.s() +270
Devart.Data.MySql.bk.d() +200
Devart.Data.MySql.v.a(ah[]& A_0, Int32& A_1) +134
Devart.Data.MySql.v.a(Byte[] A_0, Int32 A_1, Boolean A_2) +106
Devart.Data.MySql.a3.e() +169
Devart.Data.MySql.a3.o() +89
Devart.Data.MySql.MySqlCommand.InternalExecute(CommandBehavior behavior, IDisposable stmt, Int32 startRecord, Int32 maxRecords) +1472
Devart.Common.DbCommandBase.InternalExecute(CommandBehavior behavior, IDisposable stmt, Int32 startRecord, Int32 maxRecords, Boolean nonQuery) +48
Devart.Common.DbCommandBase.ExecuteDbDataReader(CommandBehavior behavior, Boolean nonQuery) +764
Devart.Common.DbCommandBase.ExecuteDbDataReader(CommandBehavior behavior) +38
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() +12
Devart.Data.Linq.Provider.DataProvider.a(b A_0, Object[] A_1, Object[] A_2, Object A_3) +1436


Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Fri 16 Apr 2010 14:31

The problem is that you perform an attempt to order the table using the completely random column number, and
table does not have so many columns as stated in the error message.
Here is the working code for Dept example:

Code: Select all

        Random rnd = new Random();
        int deptLimit = db.Mapping.MappingSource.GetModel(typeof(DataContext1.DataContext1)).GetMetaType(typeof(Dept)).DataMembers.Count(); //This code gives us the number of the columns in entity being sorted
        var deptsrandom = (from d in db.Depts
                              orderby rnd.Next(1, deptLimit) //we can perform ordering only in this range
                              select d);
        int count = deptsrandom.Count();
        short PageNumber = 1;
        var deptsrandom2 = deptsrandom.Skip((Convert.ToInt16(2) * (PageNumber - 1))).Take(Convert.ToInt16(2));
        var result = deptsrandom2.ToList();

Post Reply