当前位置:  开发笔记 > 编程语言 > 正文

如何在C#中实现linq on dictionary?

如何解决《如何在C#中实现linqondictionary?》经验,为你挑选了1个好方法。



1> Alberto Mont..:

您可以使用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 search变量包含一些类似的数据:

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 List SubCategories { 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;
    List search = 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);            
}

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有