简体   繁体   English

如何使用LINQ将查询结果返回到字典

[英]How to use LINQ to return a query result to a dictionary

I am working on an MVC project. 我正在做一个MVC项目。 I need to return an object that groups menu items into groups of four to display in the view using foreach iteration. 我需要返回一个对象,该对象将菜单项分为四个组,以使用foreach迭代显示在视图中。 As this is a model being passed to the view, I need to return a non-generic collection class that can be bound on the .cshtml. 由于这是一个传递给视图的模型,因此我需要返回一个可以绑定到.cshtml上的非通用集合类。

My thought is that I could group the list based on some type of count within LINQ to add four new List items to every Dictionary key. 我的想法是,我可以根据LINQ中的某种计数对列表进行分组,以向每个Dictionary键添加四个新的List项目。 However, I haven't found an expression that would suffice to a Dictionary. 但是,我还没有找到足以满足字典要求的表达式。 I could solve this rather easily by creating another method and iterating again through the collection to assign keys based on a count, but it seems like unneeded iteration and bad practice. 我可以通过创建另一种方法并在集合中再次进行迭代以基于计数来分配键来轻松解决此问题,但这似乎是不必要的迭代和不良实践。

Currently, I am working with the following query. 目前,我正在使用以下查询。

        var itemsDict = rssDataContext.rss_Application_Blog_Category_Relationships
            .Where(x => x.Application_Blog.rss_Application.ID == 1)
            .Where(x => x.Blog_Category_ID == 1)
            .Select(x => new MenuItem
            {
            Name = x.rss_Application_Blog.Blog_Title,
            Uri = x.rss_Application_Blog.Blog_Uri,
            ID = x.rss_Application_Blog.ID
            });

But is there a way to group this query into groups of 4 class that be populated into Dictionary<int, List<MenuItem>>? 但是,是否有一种方法可以将此查询分为4类,然后填充到Dictionary <int,List <MenuItem >>中?

I suspect you could do this: 我怀疑您可以这样做:

var itemsDict = rssDataContext.rss_Application_Blog_Category_Relationships
    .Where(x => x.Application_Blog.rss_Application.ID == 1)
    .Where(x => x.Blog_Category_ID == 1)
    .Select(x => new { 
        x.rss_Application_Blog.BlogTitle,
        x.rss_Application_Blog.BlogUri,
        x.rss_Application_Blog.ID
    })
    .AsEnumerable() // Do the rest of the query in-process
    .Select((value, index) => new { value, index })
    .ToLookup(pair => pair.index % 4,
              pair => new MenuItem {
                 Name = pair.value.Blog_Title,
                 Uri = pair.value.Blog_Uri,
                 ID = pair.value.ID
              });

Note that this returns a Lookup rather than a Dictionary , but that's actually a closer model for what you want. 请注意,这将返回Lookup而不是Dictionary ,但这实际上是您想要的更接近的模型。

@JonSkeet's solution is pretty good. @JonSkeet的解决方案非常好。 Here's an alternative implementation using GroupBy to create the sublists and ToDictionary to project them into the final result: 这是使用GroupBy创建子列表并使用ToDictionary将它们投影到最终结果中的替代实现:

var itemsDict = rssDataContext.rss_Application_Blog_Category_Relationships
    .Where(x => x.Application_Blog.rss_Application.ID == 1)
    .Where(x => x.Blog_Category_ID == 1)
    .ToArray()
    .Select((x, index) => new
        {
            Index = index,
            Item = new MenuItem
            {
                Name = x.rss_Application_Blog.Blog_Title,
                Uri = x.rss_Application_Blog.Blog_Uri,
                ID = x.rss_Application_Blog.ID
            }
        });
    .GroupBy(i => i.Index / 4)
    .ToDictionary(g => g.Key);

The resulting sequence will look something like this: 结果序列看起来像这样:

items = new Dictionary<int, IGrouping<int>>()
{
    0, new[]
    {
        new { Index = 0, Item = new MenuItem { ... } },
        new { Index = 1, Item = new MenuItem { ... } }
    }
    1, new[]
    {
        new { Index = 2, Item = new MenuItem { ... } },
        new { Index = 3, Item = new MenuItem { ... } }
    }
};

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

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