【问题标题】:Linq Select OrderBy ThenBy Kendo DropdownlistLinq 选择 OrderBy ThenBy Kendo 下拉列表
【发布时间】:2021-04-17 17:27:09
【问题描述】:

我的应用程序是 MVC5。我正在填充国家剑道下拉列表,试图将加拿大和美国置于列表顶部:

     public JsonResult GetCountries()
            {
                return Json(db.Country.OrderBy(x => x.TwoCharCountryCode == "CA")
                .ThenBy(x => x.TwoCharCountryCode == "US").ThenBy(x => x.CountryName)
             .Select(c => new { CountryId = c.CountryId, CountryName = c.CountryName }), JsonRequestBehavior.AllowGet);
            }



              @(Html.Kendo().DropDownList()
                  .Name("Country")
                  .HtmlAttributes(new { style = "width:300px;", id="Country", value = ViewBag.CountryId })
                 .OptionLabel("Select country...")
                  .DataTextField("CountryName")
                  .DataValueField("CountryId")
                  .DataSource(source =>
                  {
                      source.Read(read =>
                      {
                          read.Action("GetCountries", "Home");
                      }).ServerFiltering(true);
                  })
                  .Events(e => {e.Select("onSelect");
                  }))

加拿大和美国垫底!

我可以使用两个 ThenBy 吗?还是我做错了什么?

【问题讨论】:

    标签: asp.net-mvc entity-framework linq kendo-ui


    【解决方案1】:

    只需强制 LINQ 翻译器创建正确的查询:

    var query = db.Country
       .OrderBy(x => x.TwoCharCountryCode == "CA" 
          ? 0 
          : x.TwoCharCountryCode == "US"
          ? 1 : 2)
       .ThenBy(x => x.CountryName)
       .Select(c => new { CountryId = c.CountryId, CountryName = c.CountryName });
    

    【讨论】:

      【解决方案2】:

      当您执行 .OrderByThenBy 布尔值时,False(表示 0)将始终位于 True(表示 1)之前。

      如果您必须将加拿大和美国放在列表的顶部,然后订购其余的,我会先将这两个从列表中取出,重新排列列表的其余部分,然后将所有内容重新组合在一起:

      public JsonResult GetCountries()
      {
          var canadaOrUsa = db.Country
              .Where(x => x.TwoCharCountryCode == "CA" ||
                          x.TwoCharCountryCode == "US")
              .OrderBy(x => x.CountryName);
      
          var theRest = db.Country
              .Where(x => x.TwoCharCountryCode != "CA" &&
                          x.TwoCharCountryCode != "US")
              .OrderBy(x => x.CountryName);
      
          return Json(
              canadaOrUsa.Concat(theRest)
                  .Select(x => new {
                      CountryId = x.CountryId, 
                      CountryName = x.CountryName
                  }, JsonRequestBehavior.AllowGet);
      }
      

      如果你不想枚举列表两次,我猜你可以从你的db.Country.ToList(),找到加拿大和美国,把它们从列表中取出,点剩下的,然后放它们回到列表的开头。

      @configurator 在这里写了一个关于将项目移到列表顶部的扩展方法:https://stackoverflow.com/a/1668662/2410655

      public static IEnumerable<T> MoveToTop(IEnumerable<T> list, Func<T, bool> func) {
          return list.Where(func)
                     .Concat(list.Where(item => !func(item)));
      }
      

      您可以使用它来减少您需要编写的代码行数:

      public JsonResult GetCountries()
      {
          var result = db.Country
              .OrderBy(x => x.CountryName)
              .MoveToTop(x => x.TwoCharCountryCode == "CA" ||
                          x.TwoCharCountryCode == "US");
      
          return Json(
              result
                  .Select(x => new {
                      CountryId = x.CountryId, 
                      CountryName = x.CountryName
                  }, JsonRequestBehavior.AllowGet);
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-04
        • 1970-01-01
        • 2018-09-16
        • 1970-01-01
        相关资源
        最近更新 更多