If Statement within Linq Query

asp.net, c#, linq, sql

Solution

You mean something like this?

var items = 
    (from p in _db.Funds
     where p.DesignationId == id
     select new { p.id, p.Name });
if (id != "some id")
{
    items = items.OrderBy(p => p.Name);
}

return items.ToList();

Problem

I am attempting to sort dropdownlist just exactly how it is entered in the database only if a specific id is selected. Otherwise I want them to sorted in ascending order. I was not sure how to add the final component of not sorting the list when a particular Id is selected. Here is where i am so far: ``` var items = (from p in _db.Funds where p.DesignationId == id orderby p.Name ascending select new { p.id, p.Name }); return items; ```

Original source