开发者

Linq to SQL / C#: How to Order By using property in cross reference table with lambda expressions

开发者 https://www.devze.com 2023-01-04 14:02 出处:网络
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:

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 IQ开发者_C百科ueryable<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...


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)); 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消