Ricardo Deano
Ricardo Deano

Reputation: 2819

LINQ OrderBy query

I have the following that pulls through a list of suppliers:

public List<tblSupplierPerformance> GetSupplierInfo(string memberid, string locationid, string supplieridname)
{
    MyEntities suppliers = new MyEntities();

    var r = (from p in suppliers.tblSupplierPerformances
             where p.MemberId == memberid && p.LocationId == locationid
             orderby p.TotalPurchaseQuantity 
             select p)
            .Distinct();

    if (supplieridname != "0")
        r = r.Where(p => p.SupplierIDName == supplieridname);

    return r.ToList();
}

However, when this runs, the orderby doesn't seem to be ordering.

I think, think, I need to implement the orderby at the "return r." stage, but I don't really know how to do this or I could be very much wrong all over the shop!

Any pointers gladly received.

Upvotes: 2

Views: 23863

Answers (2)

anishMarokey
anishMarokey

Reputation: 11397

Yes, you need to implement order by in the return

return r.ToList().OrderBy(o => o.Column1);

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503329

I suspect it's the Distinct call which is messing up the ordering... although as you're not joining or doing anything like that, I'm not sure why you need distinct - isn't each entity naturally distinct?

Anyway, you could certainly move the ordering to the return statement:

return r.OrderBy(p => p.TotalPurchaseQuantity).ToList();

Upvotes: 9

Related Questions