Linq random rows

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
ymorales
Posts: 4
Joined: Wed 03 Jul 2013 20:22

Linq random rows

Post by ymorales » Wed 03 Jul 2013 20:36

Hi.
I want do that:

Code: Select all

var products = db.Styles.OrderBy(e => Guid.NewGuid()).ToList();
With Visual Studio 2012 ado.net entity data model I can do that and no error. But with you LinqConnect model I cant, give me an error.

"Error on executing DbCommand."
InnerException: "The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name."

I must get data from sql from this way:

Code: Select all

SELECT * FROM styles ORDER BY NEWID()
I dont want to use Ado.Net entity model and SQL command.
I bought you component and I like it. There is any way to do that in LinqConnect model?
Thanks

PS: My goal is get rows randomly, Im using Microsoft Sql Server

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Linq random rows

Post by MariiaI » Thu 04 Jul 2013 11:03

ymorales wrote: There is any way to do that in LinqConnect model?
Please try re-writing your query in the following way and notify us about the results:

Code: Select all

var products = db.Styles
                    .ToList()
                    .Select(e => new { results = e, Guid = Guid.NewGuid()})
                    .OrderBy(e => e.Guid)
                    .Select(e => e.results)
                    .ToList();

ymorales
Posts: 4
Joined: Wed 03 Jul 2013 20:22

Re: Linq random rows

Post by ymorales » Fri 05 Jul 2013 13:44

Nice thanks. I dont know why typing ToList() before the orderby it solve the problem.

Solution:
var products = db.Styles.ToList().OrderBy(e => Guid.NewGuid()).ToList();

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Linq random rows

Post by MariiaI » Mon 08 Jul 2013 09:19

The .ToList() method forces the execution of the query and enumerates all resulting objects to the list of results. After this OrderBy is performed on the client side via LINQ to Objects (OrderBy(e => Guid.NewGuid())).

Post Reply