views:

30

answers:

1

I am trying to order a list of products based on the zindex property of the cross reference table with the category table (in this case called 'Chassis'), but I get the following error:

Cannot order by type 'System.Collections.Generic.IEnumerable`1[System.Int32]'.

The following is the method I am using:

public IQueryable<E_Product> Product_GetList_ByChassisId(int chassisId)
{
    return dc.E_Products
        .Where(x => x.Deleted == false)
        .Where(x => x.Published == true)
        .Where(x => x.E_Product_Chassis
            .Any(c => c.ChassisId == chassisId && c.Deleted == false))
        .OrderBy(x => x.E_Product_Chassis.Select(c => c.Zindex));
}

I understand the .Select method returns an IEnumerable, but being a many-to-many relationship, x.E_Product_Chassis does not allow simple selection of its properties (e.g. x.E_Product_Chassis.Zindex).

Any help would be very appreciated...

+2  A: 

FirstOrDefault(), Min(), Max() -- use one of these functions to select the appropriate z-index out of the set.

public IQueryable<E_Product> Product_GetList_ByChassisId(int chassisId) 
{ 
    return dc.E_Products 
        .Where(x => x.Deleted == false) 
        .Where(x => x.Published == true) 
        .Where(x => x.E_Product_Chassis 
            .Any(c => c.ChassisId == chassisId && c.Deleted == false)) 
        .OrderBy(x => x.E_Product_Chassis.Min(c => c.Zindex)); 
}
tvanfosson
Heh. Duplicate answers.
Toby
"FirstOrDefault(), Min(), Max()" ... or any other method of returning a scalar value from a collection. Just wanted to note that these aren't your only selectors.
Marc
Thank you! That did the trick!!