LINQ and related types

D

Davej

Ok, so how do you pass an array of anonymous thingies ?

var qtySorted =
from e in Inventory
orderby e.Quantity
select new {PartDesc = e.PartDescription, e.Quantity} ;

displayAIOEArray(ref qtySorted);

/* this will not compile
Error Argument: cannot convert from 'ref
System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'ref
System.Linq.IOrderedEnumerable<Invoice>'
*/

private static void displayAIOEArray(ref IOrderedEnumerable<Invoice>
array)
{
if ( array.Any() )
{
foreach (var element in array)
Console.Out.WriteLine("{0}", element); //uses
class tostring override
}
else
{
Console.Out.WriteLine("No Results in array.");
}
}
 
M

Matt

Ok, so how do you pass an array of anonymous thingies ?

You can't, at least not to something expecting something OTHER
than an array of anonymous thingies (if there were such a thing).

var qtySorted =
from e in Inventory
orderby e.Quantity
select new {PartDesc = e.PartDescription, e.Quantity} ;

This creates an anonymous type (as the compiler told you)
with two fields it.
displayAIOEArray(ref qtySorted);

This, on the other hand, expects an array of Invoice, or at least some
sort of ordered enumerable type of Invoice.
/* this will not compile
Error Argument: cannot convert from 'ref
System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'ref
System.Linq.IOrderedEnumerable<Invoice>'
*/

private static void displayAIOEArray(ref IOrderedEnumerable<Invoice>
array)
{
            if ( array.Any() )
            {
                foreach (var element in array)
                    Console.Out.WriteLine("{0}", element); //uses
class tostring override
            }
            else
            {
                Console.Out.WriteLine("No Results in array.");
            }



}

I'm sure there are lots of ways to do this, but the easiest would
simply
be to copy the elements from the qtySorted list into an enumerable of
Invoice and pass it into the method.

Matt
 
A

Arne Vajhøj

Ok, so how do you pass an array of anonymous thingies ?

var qtySorted =
from e in Inventory
orderby e.Quantity
select new {PartDesc = e.PartDescription, e.Quantity} ;

displayAIOEArray(ref qtySorted);

/* this will not compile
Error Argument: cannot convert from 'ref
System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'ref
System.Linq.IOrderedEnumerable<Invoice>'
*/

private static void displayAIOEArray(ref IOrderedEnumerable<Invoice>
array)

You don't.

Anonymous types are for local usage.

For non local usage you must give it a type.

select new {PartDesc = e.PartDescription, e.Quantity}

->

select new Invoice {PartDesc = e.PartDescription, Quantity = e.Quantity}

or something like that.

Oh - and I would get rid of that ref as well. Standard
ref by value passing is fine here.

Arne
 
Top