views:

90

answers:

2

Hi there,

I found that Entity SQL support NEWID(), but does ObjectQuery support it as well? http://msdn.microsoft.com/en-us/library/bb738616.aspx,

Can I write objectquery like:

context.member.orderby("NEWID()").select("it.UserID");

or something like this? or I should write in other way?

I thought if entity sql support NEWID() function, it should be accepted by ObjectQuery also. Like you can use distinct(it.UserID), or BitWiseAND(it.UserID, 1) in ObjectQuery.Where() or Select().

Many thanks.

A: 

This is a SQL Server-specific canonical function, so it should be prefixed with 'SqlServer':

context.member.orderby("SqlServer.NEWID()").select("it.UserID");

Unfortunately, this will not work either: the Where extension method needs at least one reference to the immediate input scope.

Devart
A: 

Thanks Devart for response.

Actually, I found that I can use like:

var query1 = context.member.select("it.userid, SqlServer.NEWID() as newid").orderby("it.newid");

this can make random order for result, you will find that NEWID() is in the translated sql query.

but if you want to select partially result from 'query1' result set, you cannot write:

var query2 = context.member.select("it.userid, SqlServer.NEWID() as newid").orderby("it.newid").select("it.userid");

because when you use sql profiler to watch the sql that translated pass into sql server, you will find that the 'NEWID()' disappear.

However, I think 'query2' should be make sense. But it doesn't work.

Jeff Chen
In fact, this is an expected situation. There is an article (http://msdn.microsoft.com/en-us/library/bb896273.aspx) about Entity Framework Known Issues, it says that EF does not preserve ordering in subqueries. Try to materialize the query and then take only IDs.
Devart