您可以使用LINQ中的GroupBy方法来执行此操作,请参阅下文.
假设您的GopreadyOnlineTest类是这样的:
public class GopreadyOnlineTest { public string CategoryId { get; set; } public string CategoryName { get; set; } public string SubCategoryId { get; set; } public string SubCategoryName { get; set; } public string Question { get; set; } public string OptionA { get; set; } public string OptionB { get; set; } public string OptionC { get; set; } public string OptionD { get; set; } public string QuestionDescription { get; set; } }
并且您的List
变量包含一些类似的数据:
this.search = new List{ new GopreadyOnlineTest { CategoryName = "General Knowledge", SubCategoryName = "Basic General Knowledge" }, new GopreadyOnlineTest { CategoryName = "General Knowledge", SubCategoryName = "World Geography" }, new GopreadyOnlineTest { CategoryName = "General Knowledge", SubCategoryName = "Inventions" }, new GopreadyOnlineTest { CategoryName = "General Knowledge", SubCategoryName = "Honours and Awards" }, new GopreadyOnlineTest { CategoryName = "Maths", SubCategoryName = "Time & Speed" }, new GopreadyOnlineTest { CategoryName = "Maths", SubCategoryName = "Algebra" }, new GopreadyOnlineTest { CategoryName = "Maths", SubCategoryName = "Accounts" } };
让我们创建一个ViewModel来保存我们转换的数据
public class SampleViewModel { public string Category { get; set; } public ListSubCategories { get; set; } }
然后你的行动应该是这样的:
[HttpGet] public JsonResult GetOnlineTestTitle() { var result = search.GroupBy(x => x.CategoryName) .Select(c => new SampleViewModel { Category = c.Key, SubCategories = c.Select(sc => sc.SubCategoryName).Distinct().ToList() }); return Json(result, JsonRequestBehavior.AllowGet); }
在前端,你的javascript应该是这样的:
function GetOnlineTestTitle() { $.ajax({ type: "GET", url: 'GetOnlineTestTitle', contentType: "application/json; charset=utf-8", dataType: "json", async: true, cache: false, success: function (msg) { $("#examList").html(""); var divs = msg.map(function (item) { var $div = $(""); var $ul = $(""); var $h3 = $("
").text(item.Category); item.SubCategories.forEach(function (itemSub) { $ul.append($("
- ").text(itemSub)); }); $div.append($h3); $div.append($ul); return $div; }); $('#examList').append(divs); }, error: function (msg) { alert(JSON.stringify(msg)); } }); }
你可以在这里看到它:https://dotnetfiddle.net/PdaW67
由于上面的代码比较多,我在下面添加了几乎真实的代码:
public ActionResult getOnlineTestTitle() { var connectionString = WebConfigurationManager.ConnectionStrings["liveData"].ConnectionString; Listsearch = Session["OnlineTest"] as List ?? GopreadyOnlineTest.Search(connectionString).ToList(); Session["OnlineTest"] = search; var result = search.GroupBy(x => x.CategoryName) .Select(c => new SampleViewModel { Category = c.Key, SubCategories = c.Select(sc => sc.SubCategoryName).Distinct().ToList() }); return Json(result, JsonRequestBehavior.AllowGet); } 推荐阅读
如何解决《覆盖动画填充模式:在JavaScript/CSS中转发》经验,为你挑选了0个好方法。 ... [详细] 如何解决《如何获取byte[]以在视图上显示为div的背景图像(C#,ASP.NET,MVC)》经验,为你挑选了0个好方法。 ... [详细] 如何解决《有没有办法确定C++编译器实现的语言功能?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《如何在Seaborn热图旁边添加列》经验,为你挑选了1个好方法。 ... [详细] 如何解决《为什么TypeError上的JSON.stringify返回一个空对象》经验,为你挑选了1个好方法。 ... [详细] 如何解决《角饼干》经验,为你挑选了3个好方法。 ... [详细] 如何解决《从协议扩展实例方法中读取静态var》经验,为你挑选了1个好方法。 ... [详细] 如何解决《为什么我使用框架获得不可用的声明》经验,为你挑选了1个好方法。 ... [详细] 如何解决《将可变长度JSON数组解码为Rust数组》经验,为你挑选了1个好方法。 ... [详细] 如何解决《gitrebase是否会产生比gitmerge更多的冲突?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《单击edittext时自动滚动》经验,为你挑选了1个好方法。 ... [详细] 如何解决《多行导航栏标题》经验,为你挑选了1个好方法。 ... [详细] 如何解决《方法调用上的问号语法》经验,为你挑选了1个好方法。 ... [详细] 如何解决《select2不保存Edit的输入》经验,为你挑选了0个好方法。 ... [详细] 如何解决《无法读取属性'refs'的null反应错误反应js》经验,为你挑选了2个好方法。 ... [详细] 如何解决《如何_ts在DocumentDB中更改》经验,为你挑选了1个好方法。 ... [详细] 如何解决《JenkinsChangelogforGit标签》经验,为你挑选了0个好方法。 ... [详细] 如何解决《如何进行需要其子函数信息的回调》经验,为你挑选了0个好方法。 ... [详细] 吐了个 "CAO" !Tags | 热门标签RankList | 热门文章
- 1在TensorFlow中合并字符串张量
- 2android检测测试的输出在哪里?
- 3节点错误:"找不到模块'路由'"
- 4如何将swagger与我的快速应用程序集成
- 5cv2.ml_KNearest对象没有属性find_nearest
- 6为什么我不能使用jQuery检查这个单选按钮?
- 7什么是phpMyAdmin对我的UNION ALL查询做什么?
- 8从Python调用C Math函数的高效解决方案
- 9有没有办法在向量常量时间中插入一些东西
- 10Spring JPA不在
- 11引导程序布局-我们是否应始终使用XS col类
- 12分段故障11,因为C中有40 MB的阵列
- 13配置Django和Google云端存储?
- 14使用deleteBy和removeBy之间的JpaRepository区别
- 15Azure Web App临时文件清理责任
- 16点击不在Windows上的python 3.5中工作
- 17如何设置每个文件类型选项卡大小?
- 18命名空间标识符的范围是什么?
- 19显示缓冲异步通道和通道之间差异的示例?
- 20我需要按一个按钮来发推文
DevBox开发工具箱 | 专业的在线开发工具网站 京公网安备 11010802040832号 | 京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有