Convert Linq IOrderedQueryable to IQueryable

A

Andrus

I need to force DbLinq to generate select statement without ORDER BY.

I tried

IOrderedQueryable<T> orderedQueryable;
orderedQueryable = (IOrderedQueryable<T>) (from b in Default.Db.Klients
where region =="xx" orderby b.Kood,b.Tanav select b);
var cnt = ( (IQueryable<T>) orderedQueryable).Count();

but generated select statement still contains order by which causes
exception in Postgres.

How to fix ?

Fix this currently I pass two versions of same query to my data
retrival class, one for data and other for count

IQueryable<T> queryable = (IQueryable<T>)(from b in Default.Db.Klients where
region =="xx"
select b);

IOrderedQueryable<T> orderedQueryable = (IOrderedQueryable<T>) (from b in
Default.Db.Klients
where region =="xx" orderby b.Kood,b.Tanav select b);

var cnt = queryable.Count();
// this causes exception: var cnt = orderedQueryable.Count();

In this case where clauses are duplicated in two places of code. How to
remove this duplication ?

Andrus.
 
M

Marc Gravell

How about:
var query = from b in Default.Db.Klients where
region =="xx"
select b;

var query2 = query.OrderBy(b=>b.Kood).ThenBy(b=>b.Tanav);
int count = query.Count();
foreach(var item in query2) {
// ...
}

i.e. you build the /where/ query (and use that for the count), then
you further compose the /ordered/ query and use that for the data.

Any good?

Marc
 
M

Marc Gravell

btw, if you're doing that, you may as well use the Where() syntax too:
var query = Default.Db.Klients.Where(b=>b.Region=="xx");

also:
// this causes exception: var cnt = orderedQueryable.Count();

You should log this as a bug with the DbLinq author/authors - it
should work.

Marc
 
A

Andrus

Marc,
var query = from b in Default.Db.Klients where
region =="xx"
select b;

var query2 = query.OrderBy(b=>b.Kood).ThenBy(b=>b.Tanav);
int count = query.Count();
foreach(var item in query2) {
// ...
}

Thank you.
I need to pass those queries from my generic WinForms form constructor to
generic paged data retriever class.
I tried the following code but got error shown in comment. How to fix ?

class VirtualGridForm<T>: Form {

publicVirtualGridForm() {

IOrderedQueryable<T> orderedQueryable;
IQueryable<T> queryable;

switch ( typeof(T).Name ) {

case "Klient":
queryable = (IQueryable<T>)(from b in Default.Db.Klients select b);

//Error: 'T' does not contain a definition for 'Kood' and no extension
method 'Kood' accepting a first argument
// of type 'T' could be found (are you missing a using directive or an
assembly reference?)
orderedQueryable = queryable.OrderBy(b => b.Kood).ThenBy(b=>b.Tanav);
break;

case "Strings":
// this is ok but where clause needs to be duplicated if present.
queryable = (IOrderedQueryable<T>)(from b in Default.Db.Strings select b);
orderedQueryable = (IOrderedQueryable<T>)(from b in Default.Db.Strings
orderby b.Est
select b);
break;

default:
throw new ApplicationException("No query for table");
}

DataRetriever<T> DataRetriever = new DataRetriever<T>( queryable,
orderedQueryable);

}}



/// data retriever class based on MSDN Virtual DataGridView sample.
class DataRetriever<T> : IDataPageRetriever<T> {
IQueryable<T> Queryable;
IOrderedQueryable<T> OrderedQueryable;

internal DataRetriever(IQueryable<T> queryable,
IOrderedQueryable<T> orderedQueryable) {
Queryable = queryable;
OrderedQueryable = orderedQueryable;
}

public IList<T> SupplyPageOfData(int lowerPageBoundary, int rowsPerPage) {
return OrderedQueryable.Skip(lowerPageBoundary).Take(rowsPerPage).ToList();
}}


Btw. in ADO .NET I can simply use


DataRetriever<T> DataRetriever = new DataRetriever<T>( "SELECT * FROM
Klient" ,
" ORDER BY Kood, Tanav" );
 
M

Marc Gravell

I tried the following code but got error shown in comment. How to fix ?
Well, I could build the property-access as an Expression, but IIRC
DbLinq had problems with that before (with dynamic Where clauses).

Compile-time LINQ (i.e. using the MethodInfo etc form) doesn't play
well with generic methods simply on "T" since you can't really do much
with a lambda just on "T", and DbLinq didn't (last time we chacked)
get on well with the Expression form, even though LINQ-to-SQL was
perfectly happy...

I wonder if something a bit more like a factory would be a better
approach here - i.e. in the switch calling out to your specific (fully
typed) methods to build the query/queries... it would need a little
casting, but much less pain than string-based expressions... just a
thought...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top