Nice Simple ASP.NET MVC2 C# Question (Switch Statement)
Good Morning,
I have the following line of code:
var viewModel = new ClassifiedsBrowseViewModel
{
Category = categoryModel,
Listings = categoryModel.Listings.OrderBy(c => c.Price).ToList()
};
All is good in the world, and this code works fine. But now I would like to add a switch statement that allows me to change what I am ordering listings by (in the above code it is ordered by price).
So I obviously cannot but the switch statement within the new model deceleration, so I will be putting the switch statement above e.g.:
switch(searchCriteria)
{
case "Price":
break;
case default:
break;
}
So my question is how do I link the two? BTW listing is declared within the ViewModel as:
public List<Listing> Listings { get; set; }
I thought I could declare a Listings variable in the Controller and then set that variable in the Switch to categoryModel.Listings.OrderBy(c => c.[Search Criteria]).ToList() and then simply have Listings = Listings ?
Many Thanks, J
Using
Using Ribon's Method:
var viewModel = new ClassifiedsBrowseViewModel
{
开发者_JAVA技巧 Category = categoryModel,
Listings = categoryModel.Listings.OrderBy(c =>
{
switch (searchCriteria)
{
case "Price": return c.Price;
case "FuelType": return c.FuelType;
default: return c.Price;
}
}).ToList()
};
IMO a nice, clean and readable solution would be to create an extension method that accepts an IQueryable, and an enumeration which would represent the various ordering options.
public static IOrderedQueryable<Listing> WithListingOrder(this IQueryable<Listing> source, PriceOrderingOptions orderBy)
{
switch (orderBy)
{
case ListingOrderingOptions.Price:
return source.OrderBy(x => x.Price);
... // other enumerations
}
}
I have a generic ordering extension method (taking a T,TKey), but in your case since your working with a string (search criteria), can't use generics here.
But i think expressing the ordering options as an enumeration should prevent "magic strings".
And use it like this:
var searchCritera = ListingOrderingOptions.Price;
var viewModel = new ClassifiedsBrowseViewModel
{
Category = categoryModel,
Listings = categoryModel.Listings.WithListingOrder(searchCriteria)
}).ToList()
};
The main benefit here is your hiding the ugly switch behind the extension - the goal here is to keep your controller clean.
You could use your linq statement in your switch statement such as
switch(searchCriteria)
{
case "Price":
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.Price).ToList()
break;
case default:
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.SomeOtherField).ToList()
break;
}
I would have:
IEnumerable<Foo> listings = categoryModel.Listings;
switch(sortOrder) {
case "x":
listings = listings.OrderBy(l => l.Something);
break;
case "y":
listings = listings.OrderBy(l => l.Whatever);
break;
}
And then use the listings variable in the model creation:
...
Listings = listings
...
( or listings.ToList() )
I know nothing about Linq statements, bu can't you just do the folowing ?
var viewModel = new ClassifiedsBrowseViewModel
{
Category = categoryModel,
Listings = categoryModel.Listings.OrderBy(c => {
switch (searchCriteria) {
case "Price" : return c.Price; break;
default: return c; break;
}
}).ToList()
};
精彩评论