简体   繁体   English

不错的简单ASP.NET MVC2 C#问题(切换语句)

[英]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). 但是现在我想添加一个switch语句,该语句允许我更改订购商品的方式(在上面的代码中按价格订购)。

So I obviously cannot but the switch statement within the new model deceleration, so I will be putting the switch statement above eg: 所以我显然只能在新模型减速中使用switch语句,因此我将switch语句放在例如:

  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: BTW列表在ViewModel中声明为:

 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 ? 我以为我可以在Controller中声明一个Listings变量,然后在Switch中将该变量设置为categoryModel.Listings.OrderBy(c => c。 [Search Criteria] )。ToList(),然后简单地让Listings = Listings吗?

Many Thanks, J 非常感谢,J

Using 运用

Using Ribon's Method: 使用Ribon方法:

 var viewModel = new ClassifiedsBrowseViewModel
        {
            Category = categoryModel,
            Listings = categoryModel.Listings.OrderBy(c =>
            {
                switch (searchCriteria)
                {
                    case "Price": return c.Price;
                    case "FuelType": return c.FuelType;
                    default: return c.Price;
                }
            }).ToList()
        };

I know nothing about Linq statements, bu can't you just do the folowing ? 我对Linq语句一无所知,但是您不能只做下面的事情吗?

var viewModel = new ClassifiedsBrowseViewModel
        {
            Category = categoryModel,
            Listings = categoryModel.Listings.OrderBy(c => {
                switch (searchCriteria) {
                    case "Price" : return c.Price; break;
                    default: return c; break;
                }
            }).ToList()
        };

You could use your linq statement in your switch statement such as 您可以在switch语句中使用linq语句,例如

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 = listings
...

( or listings.ToList() ) (或listings.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. IMO一个不错的,干净的和易读的解决方案是创建一个扩展方法,该方法接受IQueryable和一个表示各种排序选项的枚举。

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. 我有一个通用的排序扩展方法(采用T,TKey),但是在您的情况下,由于您使用的是字符串(搜索条件),因此不能在此处使用通用。

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. 这样做的主要好处是您可以将扩展开关后面的难看的开关隐藏起来 -目的是保持控制器清洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM