Joe Rattz

There is a prototype for the CreateQuery method that returns an IQueryable, as opposed to IQueryable<T>. I can't seem to find a way to enumerate through an IQueryable. How is it done

Thanks.




Re: LINQ Project General How do you enumerate an IQueryable?

Matt Warren - MSFT

IQueryable derives from IEnumerable.




Re: LINQ Project General How do you enumerate an IQueryable?

Joe Rattz

Yeah, I know that, but I still can't seem to enumerate through an IEnumerable. Here is a method I added to my entity classes that were generated by SQLMetal for Northwind:

[Function(Name="[dbo].[MinUnitPriceByCategory]")]
public IQueryable MinUnitPriceByCategory2(System.Nullable<int> categoryID) {
MethodCallExpression mc = Expression.Call(((MethodInfo)(MethodInfo.GetCurrentMethod())), Expression.Constant(this), new Expression[] {
Expression.Constant(categoryID, typeof(System.Nullable<int>))});
return this.CreateQuery(typeof(Decimal), mc);
}

It is pretty much like the existing method that was generated to call that stored procedure, except I appended "2" to the name, and it is returning IQueryable instead of decimal, and I am returning the entire sequence, as opposed to calling the Single operator, only because I can't seem to get anything out of the IQueryable. I wonder if I am passing the rowType correctly in the CreateQuery call. The stored procedure returns type Money. The MS doc maps Money to decimal, not to mention the already existing generated method to call this stored procedure seems to have no problem getting the decimal from the results. So, I feel like my rowType is correct. unless I am totally misinterpretting it.

I call the code like this:

Northwind db =
new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;");

IQueryable q = db.MinUnitPriceByCategory2(1);
IEnumerator ie = q.GetEnumerator();
while(ie.MoveNext())
{
Console.WriteLine(ie.Current);
}

That compiles clean, but when it runs, it throws:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Decimal' to type 'System.Query.IQueryable'.
at System.Data.DLinq.ProviderBase.DataRowReader.<GetFieldReader>b__18[T](Int32 i)
at System.Data.DLinq.ProviderBase.CompletingRowReader.CompletingFieldReader`1.Read(Int32 i)
at System.Data.DLinq.ProviderBase.FieldReader`1.Read(ReadSession session, RowReader reader)
at System.Data.DLinq.SqlClient.SqlQuerySession`1.SqlQueryEnumerable.SqlQueryEnumerator.get_Current()
at System.Data.DLinq.SqlClient.SqlQuerySession`1.SqlQueryEnumerable.SqlQueryEnumerator.System.Collections.IEnumerator.get_Current
()
at LINQChapter6.Program.Listing6_BO() in c:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2005\Projects\LINQ
Chapter6\LINQChapter6\Program.cs:line 524
at LINQChapter6.Program.Main(String[] args) in c:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2005\Project
s\LINQChapter6\LINQChapter6\Program.cs:line 113

Thanks.






Re: LINQ Project General How do you enumerate an IQueryable?

Matt Warren - MSFT

I think the materializer in that version of DLINQ was only recognizing IQueryable<T> and IEnumerable<T> as return types for stored-procs/table-value functions. It is getting confused thinking you are expecting an 'IQueryable' value for each row. Declaring the return type of your method as IQueryable<decimal> should fix the problem.