Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Convert Linq  IOrderedQueryable to IQueryable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrus - 12 Jan 2008 21:28 GMT
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.
Marc Gravell - 12 Jan 2008 23:12 GMT
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
Marc Gravell - 12 Jan 2008 23:40 GMT
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
Andrus - 13 Jan 2008 10:18 GMT
Marc,

> var query = from b in Default.Db.Klients where
>  region =="xx"
[quoted text clipped - 5 lines]
>  // ...
> }

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" );
Marc Gravell - 13 Jan 2008 15:13 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.